Regex too greedy Java [duplicate] - java

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

Related

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

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

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