Regex pattern throwing PatternSyntaxException in Java [duplicate] - java

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.

Related

Replacing one character in string with another string excluding pattern [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 5 years ago.
I need to replace all '%' characters in string with "%25" but I don't want to replace % in %25 to avoid situation, when I get something like this %%25.
I want to do it in Java.
Example:
input: % sdfsdaf %25
expected result: %25 sdfsdaf %25
Do you know what should I use to get it?
Without discussing much and digging deep into what you are really trying to accomplish with the replacement ... the answer to your question could be something like :
myString.replaceAll("%(?!25)", "%25");
From Pattern Documentation ; The (?!...) part means "only match if the text following (hence: lookahead) this doesn't (hence: negative) match this. But it doesn't actually consume the characters it matches (hence: zero-width).
Section 3.5 of the Link clarifies it in a bit more detail.

Regex find digits after String [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 6 years ago.
From the two urls
https://twitter.com/realDonaldTrump/status/829684271812067328
https://twitter.com/realDonaldTrump/status/829684271812067328/
I want to extract the String 829684271812067328 in Java.
My attempt was
\\/status\\/([\\d]+)
but this does not allow anything before /status or after the digits (/).
Whats the solution to this?
If you just need to extract data (rather than validate the url format), I'd use the following regex :
(\\d+)/?$
And extract the first group of the result.
It matches a non-empty sequence of digits that can be followed by a / and must be found at the end of the matched text.

This pattern matches for input 123456789.2.2.2 , which it should not [duplicate]

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
"(...)\\.(...)\\.(...)\\.(...)"

Matching module code using regex [duplicate]

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.

Java RegEx removing "/*....*/" and "{.....}" [duplicate]

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 (\/\*.*\*\/|\{.*\})

Categories

Resources