I am writing a regex to capture SQL Injection Keywords on the HTTP request.
I am having problems with allowing 0 or more carriage returns.
I have tried \s but I get the error 'errorInvalidEscaperChar'.
If I put \s then this will be taken as a literal \s.
Any ideas please?
Thanks
N
You'll have to escape the \ in your string. try "\\s".
You have to escape the backslash in Java Strings. So, to get a backslash followed by an s, you need to write: "\\s".
But let's take a step back, what are you trying to achieve? Searching in parameters for "SQL Injection Keywords" might be a bad idea. Could you provide some details?
"\" is a citation character in regexp and Java. So you need to write \\s
Related
Just something I don't understand the full meaning behind. I understand that I need to escape any special meaning characters if I want to find them using regex. And I also read somewhere that you need to escape the backslash in Java if it's inside a String literal. My question though is if I "escape" the backslash, doesn't it lose its meaning? So then it wouldn't be able to escape the following plus symbol?
Throws an error (but shouldn't it work since that's how you escape those special characters?):
replaceAll("\+\s", ""));
Works:
replaceAll("\\+\\s", ""));
Hopefully that makes sense. I'm just trying to understand the functionality behind why I need those extra slashes when the regex tutorials I've read don't mention them. And things like "\+" should find the plus symbol.
There are two "escapings" going on here. The first backslash is to escape the second backslash for the Java language, to create an actual backslash character. The backslash character is what escapes the + or the s for interpretation by the regular expression engine. That's why you need two backslashes -- one for Java, one for the regular expression engine. With only one backslash, Java reports \s and \+ as illegal escape characters -- not for regular expressions, but for an actual character in the Java language.
Funda behind extra slashes is that , first slash '\' is escape for the string and second slash '\' is escape for the regex.
I need a regexp, that matches \n everywhere, except \\\\\n (n with multiple \) Every other variant is suitable.
For example:
SOMETEXT\nANOTHERTEXT - want \n here to match my regexp
\\\\\\\\\\\\\\\n - this shouldn't match
\\\\\\\sdfsdfsdf\\\n - this shouldn't match
\\\\\\s\n - this should match
\n - this should match
Sorry, if it's a dumb question, I tried googling, but with no success
I believe you are looking for a Negative Lookbehind here.
(?<!\\)\\n
See Demo
Tried using "\\n" as a pattern?
For clarification the first \ is just for escaping special character
You need negative lookahead. Try something like this: (?!\\)\n. Probably you have to duplicate the slashes, i.e. write something like (?!\\\\)\n
Basically, for this regex
{(\(\(("\w{1,}",{0,1}){2}\),\(("[^:=;#"\)\(\{\}\[\]]{1,}",{0,1}){2}"[LR]{1}"\)\),{0,1}){1,}}
Which I've tested on Regexpal for this input:
{(("st0","sy0"),("st1","sy3","L")),(("st0","sy0"),("st1","^","L"))}
I now need in Java. I can't seem to figure out how to convert it. Can somebody show me how to?
You need to escape the special chars - specifically the backslashes and the quote marks.
The regular expression could work on Java, the only thing that you have to do, is escape the backslash .
split this String using function split. Here is my code:
String data= "data^data";
String[] spli = data.split("^");
When I try to do that in spli contain only one string. It seems like java dont see "^" in splitting. Do anyone know how can I split this string by letter "^"?
EDIT
SOLVED :P
This is because String.split takes a regular expression, not a literal string. You have to escape the ^ as it has a different meaning in regex (anchor at the start of a string). So the split would actually be done before the first character, giving you the complete string back unaltered.
You escape a regular expression metacharacter with \, which has to be \\ in Java strings, so
data.split("\\^")
should work.
You need to escape it because it takes reg-ex
\\^
Special characters like ^ need to be escaped with \
This does not work because .split() expects its argument to be a regex. "^" has a special meaing in regex and so does not work as you expect. To get it to work, you need to escape it. Use \\^.
The reason is that split's parameter is a regular expression, so "^" means the beginning of a line. So you need to escape to ASCII-^: use the parameter "\\^".
I'm using a system where I need to enter hundreds of RegEx expressions. I've recently changed a few things and am getting the following error:
java.lang.RuntimeException: ?+* follows nothing in expression
I've no idea what this means and would really appreciate any pointers for what I should be looking for to fix it.
Many thanks :)
Katie
The obvious interpretation is that you have a regex that starts with a '?', a '+' or a '*' meta-character. Maybe it should have been escaped. Maybe you've accidentally deleted the preceding things that is "quantified" by the meta-character.
I do have a '*' at the beginning of some expressions - is that bad?
Yup. If that is supposed to match a literal asterisk character, it must be preceded by a '\' to escape it. (And as Felix Kling pointed out, the '\' will itself need to be escaped if the regex is embedded in a Java string literal.)
Should I be putting '.*' (ie. dot star) instead?
It depends what you want the regex to match at that point. '.*' means "eagerly match zero or more characters". If that's what you mean, that's what you should use.
It means you have a quantifier (+,?, or *) that isn't quantifying anything. My guess is you might have forgotten to escape one of those characters (with \) when trying to match it.