This question already has answers here:
Java doesn't work with regex \s, says: invalid escape sequence
(3 answers)
Closed 6 years ago.
I have never worked with RegEx and have been trying to perform validation to ensure a module code matches the correct format. A valid module code should be in the form: CSC8001
My code is as follows:
if(moduleCode.matches("^CSC8\d{3}")){
throw new IllegalArgumentException();
}
This produces an invalid escape sequence error which I have been unable to resolve.
Thanks in advance, Mark.
You must use:
moduleCode.matches("^CSC8\\d{3}")
\d is an illegal character. To make it \d you must use \\d.
\\ escapes to form a single back slash.
Related
This question already has answers here:
Regular Expressions: How to Express \w Without Underscore
(7 answers)
Closed 2 years ago.
Currently have this regex string in my java code:
^[\\w\\-\\ \\#\\.\\/]{0,70}$
It successfully accepts those characters, however it also accepts underscore, how can modify the regex to reject underscore appearing anywhere in the string?
Your regex is:
^[\\w\\-\\ \\#\\.\\/]{0,70}$
It is using \w which is equivalent of [a-zA-Z0-9_], hence it allows underscore also.
You can change your character class to this:
^[-#. a-zA-Z0-9\\/]{0,70}$
Note that space, dot, #, / don't need to be escaped inside [...] and - if placed at first or last position doesn't require escaping either.
This question already has answers here:
Allow - (dash) in regular expression
(3 answers)
Closed 3 years ago.
I have a regex pattern which checks for client auth domain name in certificate matching the pattern.
However it is throwing patternsyntax exception.
Pattern am using is below:
^(?!\s)([a-zA-Z0-9.-\s]{1,128})$
Exception is invalid character range near index 21. I suppose it is for -/s in the range. Is there a way to change the regex pattern? Can I use -/s at start of character range? Help would be appreciated.
You just need to escape the - symbol if you are trying to match it. So the correct regex would look as follow:
^(?!\s)([a-zA-Z0-9.\-\s]{1,128})$
I suggest you to use one of many online available Regex tools when you want to learn and build your regex.
This question already has answers here:
Regex - Should hyphens be escaped? [duplicate]
(3 answers)
Closed 5 years ago.
I've tried different ways to make the special character '-' work, but it doesn't seem to work when I try to test the regex within the code. The examples I tried were these
[^(a-zA-Z0-9\\\\##$%!.'{}_-~`())]
The above one would not work as it searches for characters between '_' to '~'. Then I tried with
[^(a-zA-Z0-9\\\\##$%!.'{}_~`()\\-)]
[^(a-zA-Z0-9-\\\\##$%!.'{}_~`())]
[^(a-zA-Z0-9\\-\\\\##$%!.'{}_~`())]
None of the above seem to be working if I give '-' in the string to search. The above expressions work when I try to test in regex tester online.
EXTERNAL_USER_INVALID_PATTERN = "[^(a-zA-Z0-9\\\\##$%!.'{}_~`()\\-)]"
Pattern p = Pattern.compile(X.EXTERNAL_USER_INVALID_PATTERN);
if(p.matcher(objectName).find() || objectName.length()> X.EXTERNAL_USER_MAXLENGTH){
throw new BusinessException("The group name does not conform to specification");
}
It always throws the given exception. All the other combinations without '-' seems to be working.
Hyphen characters have special meaning inside character classes. To include it, put it as first or as last character in the class. E.g. [-A-Z] or [^A-Z-].
This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 6 years ago.
I am trying to solve following task:
Match the pattern abc.def.ghi.jkl, where each variable a,b,c,d,e,f,g,h,i,j,k,l can be any single character except the newline.
For above question I am matching the input to regex :
"([^\\n]{3}(.)){3}([^\\n]{3})"
// this is the regex pattern I am using currently
What am I doing wrong? Please help me correct the above regex so that it does not match the incorrect input I have provided in the title. Currently it matches to it somehow. Although I have provided 3 it is apparently matching to more than 3 characters.
. has a special meaning in regular expression patterns.
If you want to get a "simple dot", you need to quote/escape it (as "\\.").
And that special meaning is (under normal configuration) "any character except line breaks", which exactly matches your other condition, so you can simplify this to
"(...)\\.(...)\\.(...)\\.(...)"
This question already has an answer here:
Java - Regex - Remove comments
(1 answer)
Closed 7 years ago.
I get the error: "Invalid ecape sequence on this regex:
(\/\*[^/*]*(?:(?!\/\*|\*\/)[/*][^/*]*)*\*\/)|(\{.*?\})
Are there any other regexes that are more suitable or what can I do to fix this regex?
You need to escape the backslashes one more time. This is a "feature" of Java's strings. Java "consumes" the backslashes that you have written because it recognizes special characters like '\t'. When it sees, for example '\/' at the beginning of your regex, it thinks you're asking for a special character, and it complains because this sequence is not valid for that purpose. To get the backslash considered in the regex, you need '\\'.
That being said, this entire approach to handling comments and braces is not going to work generally because it's going to have trouble with a variety of cases like nested blocks in braces. (Just to name one of many.)
(\/\*[^\/*]*(?:(?!\/\*|\*\/)[\/*][^\/*]*)*\*\/)|(\{.*?\})
This is the correct regex, you missed escaping the forward slashes which represent the start and end of a regex sequence.
Here is a simplified version (\/\*.*\*\/|\{.*\})