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

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

Related

Split by : but not :: [duplicate]

This question already has answers here:
Regexp to remove specific number of occurrences of character only
(2 answers)
Closed 2 years ago.
I was wondering how I could split a String by : but not :: using String#split(String)
I am using Java if it makes a difference.
I looked around a lot and I couldn't find anything, and I'm not familiar with Regex...
Example:
coolKey:cool::value should return ["coolKey", "cool::value"]
cool::key:cool::value should return ["cool::key", "cool::value"]
You could try splitting on (?<!:):(?!:):
String input = "cool::key:cool::value";
String[] parts = input.split("(?<!:):(?!:)");
System.out.println(Arrays.toString(parts));
This prints:
[cool::key, cool::value]
The regex used here says to split when:
(?<!:) the character which precedes is NOT colon
: split on colon
(?!:) which is also NOT followed by colon

Java Regex match string if the patterns ends with ; or nothing [duplicate]

This question already has answers here:
Alternation operator inside square brackets does not work
(2 answers)
In regex, match either the end of the string or a specific character
(2 answers)
Closed 3 years ago.
[Posting this question because I could not find any question matching my scenario, please point me to the post if this is already discussed, I will delete this post.]
Trying to create a regex to match string app=myApp in long string separated by either , or ;.
My regex fails if the patterns is at end and not terminated by by either , or ;.
This is the regex I have used: [^.][app|APP]=(.*?)[,|;] this works for the following strings:
env=prod;app=myApp;app.secure=yes
app=myApp;app.secure=yes
But does not work for following:
env=prod;app=myApp
app=myApp
Here is my code:
Pattern pattern = Pattern.compile("[^.][app|APP]=(.*?)[,|;]");
Matcher matcher = pattern.matcher(stringVar);
if (matcher.find()) {
return matcher.group(1);
}
I have also tried:
[^.][app|APP]=(.*?)[,|;|$]
but still no luck.
Try Regex: (?:app|APP)=(.*?)(?=,|;|$)
Demo

Convert regex into java regex [duplicate]

This question already has answers here:
Regexp Java for password validation
(17 answers)
Closed 5 years ago.
string should not be longer than 26 alphanumeric characters
string should not begin with www OR api OR admin
string may contain hyphens
I have this regular expression that works:
^(?!www)(?!admin)(?!api)[a-zA-Z0-9.]{1,26}
Can you help me convert that regex into a java style string regex?
I found the answer by changing my regex to the following:
^(www|api|admin)\w{1,26}$

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>";

java regex to not get string not ending with dot [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 8 years ago.
I m trying regex to get the strings
starting with # and
not ending with a dot(.)
For that i tried the java code(link here) but this does not show any results -
#(\\w+)*(?<!.(.))*$
The string i m trying is -
This is a test\nAnother #pradyut#test ht#html.com\ntest\n#art\n#cool#paintings#collections
This should return
pradyut
test
cool
The result html ending with a .com should not return.
Regards
You can use this regex:
(?<=#)\w+\b(?!\.)
In Java you have to use:
(?<=#)\\w+\\b(?!\\.)
Regex Demo

Categories