Java regex matching with or clause [duplicate] - java

This question already has answers here:
Java regular expressions and dollar sign
(5 answers)
Closed 6 years ago.
I've got the following code
String valuepairName = "sc_mpl_MAX_AUD_TIME_To_20_Val.$$DATE_TO_LOAD";
boolean result = valuepairName.matches("sc_mpl_MAX_AUD_TIME_To_(20|32|82)_Val.$$DATE_TO_LOAD");
The result evaluates to false, but I cannot see the mistake.
This must be very trivial, hence it's driving me crazy :(

You need to escape each $ individually (or use pattern.quote) ($ is a special character in regex). Use
boolean result = valuepairName.matches("sc_mpl_MAX_AUD_TIME_To_(20|32|82)_Val.\\$\\$DATE_TO_LOAD");

Related

String#replaceFirst not working seemingly randomly [duplicate]

This question already has answers here:
Not able to replace all for dollar sign
(4 answers)
Closed 3 years ago.
I have a string, which I try to replace the first appearance of %s.
For example:
"$%s".replaceFirst("%s", "10");
returns $10, but
"&cYou do not have %s!".replaceFirst("%s", "$10");
throws java.lang.IndexOutOfBoundsException: No group 1
I have print statements, I know 100% that is what is throwing the error, and that is what is going into the statement.
The replacement containing a $ prefix is considered as group matcher (i.e. it tries to replace only matching group.
So the $ needs to be escaped with \\:
System.out.println("&cYou do not have %s!".replaceFirst("%s", "\\$10"));

java Complex Regex doesn't work in String.matches() [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 4 years ago.
i have this regex which i want to check if string do contain this structure
none of the strings below match , why?
String s = "T4018.test.12"
or
String s = "T4018.foo.32"
or
String s = "develop"
if(s.matches("T40[0-9][0-9][.][a-zA-Z]+|develop"))
{
//Never matches
}
its do match :
https://regex101.com/r/CSLugb/1
UPDATE
Solved it using Pattern.compile
Maybe you can rewrite the RegEx to "T40\\d{2}\\.\\w+\\.\\d{2}|develop" for matching more exactly. See your 3 test-cases matched in Java

Java Regular Expression - Match for anything but specific string [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 6 years ago.
How would I write a Java regular expression that matches everything except the following string?
abc123DEF#domain.com
(?!abc123DEF#domain.com) doesn't work.
Just a simple change of your regex:
(?!abc123DEF#domain.com).*
it works.

Stripping specific chars from beginning/ending of a string [duplicate]

This question already has answers here:
Regex to trim hyphens from start and end of a string
(2 answers)
Closed 7 years ago.
Given a word-string in Java, I want to strip off from beginning and from end, exactly these specified set of characters:
[?:!.,;'\"«»]
as many times as they appear.
For instance, «Be!!» should become just Be, "Here!!!" should become Here, «I should become I.
Can anyone provide a correct way to do this?
Use an anchored regex in string.replaceAll function.
string.replaceAll("^[?:!.,;'\"«»]+|[?:!.,;'\"«»]+$", "");
DEMO

Regex too greedy Java [duplicate]

This question already has answers here:
Regular expression to stop at first match
(9 answers)
Closed 7 years ago.
I'm trying to retrieve some parts from a single line html string.
The Regex I wrote is as follows:
String pattern = "rel=\"bookmark\">(.*?)</a></h2>"
The html line is like:
....rel="bookmark">what I need</a></h2>....rel="bookmark">....rel="bookmark">What I also need</a></h2>....
My Regex has two results:
rel="bookmark">what I need</a>
and
rel="bookmark">....rel="bookmark">What I also need</a></h2>
How can I tell it to be less greedy for the second result?
Between tags are the things does not contains < and >
String pattern = "rel=\"bookmark\">[^<>]+</a>";

Categories