Matcher java doesn't work but regex seems to be good - java

I want to find instance of regex in my string.
But it doesn't work.
My regex seems to be good.
My string is like that :
LB,32736,0,T,NRJ.POMPES_BACHE.PUISSANCE_ELEC_INST,20190811T080000.000Z,20190811T194400.000Z
TR,NRJ.POMPES_BACHE.PUISSANCE_ELEC_INST,0,65535,1,1,,0,0,2
20190811T080000.000Z,0.00800000037997961,192
20190811T080100.000Z,0.008999999612569809,192
20190811T080200.000Z,0.008999999612569809,192
LB,32734,0,T,NRJ.POMPES_BACHE.PUISSANCE_ELEC_CPT,20190811T080000.000Z,20190811T201200.000Z
TR,NRJ.POMPES_BACHE.PUISSANCE_ELEC_CPT,0,65535,1,1,,0,0,2
20190811T080000.000Z,0.6743068099021912,192
20190811T080100.000Z,0.6744459867477417,192
20190811T080200.000Z,0.6745882630348206,192
20190811T080300.000Z,0.6747232675552368,192
20190811T080400.000Z,0.6748600006103516,192
20190811T080500.000Z,0.6749916672706604,192
20190811T080600.000Z,0.6751362681388855,192
And I want to match only lines which have this format
20190811T080000.000Z,0.00800000037997961,192
So I have tried this regex
^([^,]*,){2}[^,]*$
And work on this website : https://regex101.com/r/iIbpgB/3
But, when I implement it on Java, it doesn't work.
Pattern pattern = Pattern.compile("^([^,]*,){2}[^,]*$");
Matcher matcher = pattern.matcher(content);
if ( matcher.find()){
System.out.println(matcher.group());
}
You can verify here : https://www.codiva.io/p/e83bcde1-8528-4330-94a2-58fe80afffc0
Someone have an explain?..
Thanks

Your are missing MULTILINE mode in your Java regex, you may use:
Pattern pattern = Pattern.compile("^([^,]*,){2}[^,]*$", Pattern.MULTILINE);
or else use inline:
Pattern pattern = Pattern.compile("(?m)^([^,]*,){2}[^,]*$");

You changed between if and while, do you want 1 match or all of them?
Pattern pattern = Pattern.compile("^([^,]*,){2}[^,]*$");
Matcher matcher = pattern.matcher(content);
while ( matcher.find()){
System.out.println(matcher.group());
}
This version will keep looping while the matcher continues to match the regex in the content string.

Related

Java Regex : Extract a specific pattern from a string "I_INSERT_TO_TOPIC_345674_123456_4.json"

I want to extract only "_123456_4" from this string using java Regex.
I_INSERT_TO_TOPIC_345674_123456_4.json
I have tried
Pattern.compile("(_([^_]*_[^_]))") and Pattern.compile("_" + "([^[0-9]]*)" + "_[0-9]") but these do not work.
If you want to get 2 group of digits just before .json then you can use regex group to find the required match. You can modify the pattern as per your requirement.
Pattern p = Pattern.compile("(_\\d+_\\d+)\\.json");
Matcher matcher = p.matcher(s);
if (matcher.find()) {
String group = matcher.group(1);
}
【\_[0-9]\*\_[0-9]\*(?=\\.)】
You can try to see if this works

extract a set of a characters between some characters

I have a string email = John.Mcgee.r2d2#hitachi.com
How can I write a java code using regex to bring just the r2d2?
I used this but got an error on eclipse
String email = John.Mcgee.r2d2#hitachi.com
Pattern pattern = Pattern.compile(".(.*)\#");
Matcher matcher = patter.matcher
for (Strimatcher.find()){
System.out.println(matcher.group(1));
}
To match after the last dot in a potential sequence of multiple dots request that the sequence that you capture does not contain a dot:
(?<=[.])([^.]*)(?=#)
(?<=[.]) means "preceded by a single dot"
(?=#) means "followed by # sign"
Note that since dot . is a metacharacter, it needs to be escaped either with \ (doubled for Java string literal) or with square brackets around it.
Demo.
Not sure if your posting the right code. I'll rewrite it based on what it should look like though:
String email = John.Mcgee.r2d2#hitachi.com
Pattern pattern = Pattern.compile(".(.*)\#");
Matcher matcher = pattern.matcher(email);
int count = 0;
while(matcher.find()) {
count++;
System.out.println(matcher.group(count));
}
but I think you just want something like this:
String email = John.Mcgee.r2d2#hitachi.com
Pattern pattern = Pattern.compile(".(.*)\#");
Matcher matcher = pattern.matcher(email);
if(matcher.find()){
System.out.println(matcher.group(1));
}
No need to Pattern you just need replaceAll with this regex .*\.([^\.]+)#.* which mean get the group ([^\.]+) (match one or more character except a dot) which is between dot \. and #
email = email.replaceAll(".*\\.([^\\.]+)#.*", "$1");
Output
r2d2
regex demo
If you want to go with Pattern then you have to use this regex \\.([^\\.]+)# :
String email = "John.Mcgee.r2d2#hitachi.com";
Pattern pattern = Pattern.compile("\\.([^\\.]+)#");
Matcher matcher = pattern.matcher(email);
if (matcher.find()) {
System.out.println(matcher.group(1));// Output : r2d2
}
Another solution you can use split :
String[] split = email.replaceAll("#.*", "").split("\\.");
email = split[split.length - 1];// Output : r2d2
Note :
Strings in java should be between double quotes "John.Mcgee.r2d2#hitachi.com"
You don't need to escape # in Java, but you have to escape the dot with double slash \\.
There are no syntax for a for loop like you do for (Strimatcher.find()){, maybe you mean while

How to create a regEx to extract value from a string with square brackets in Java

I have a string "Something[Anything]".
First I want to check whether string contains Square brackets if yes then I want this string to be separated in "Something" & "[Anything]".
Need some help with regEx for this.
Thanks in Advance.
Try this:
String test = "Something[Anything]";
if (test.matches(".*\\[.*\\].*")) { // checks if in the string presents open and close square brackets
Pattern pat = Pattern.compile("(.*?)(\\[.*?\\])");
Matcher matcher = pat.matcher(test);
matcher.find();
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
}
Outputs:
Something
[Anything]
Or, as suggested by #madatx, without first check:
Pattern pat = Pattern.compile("(.*?)(\\[.*?\\])");
Matcher matcher = pat.matcher(test);
if (matcher.find()) {
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
}
Same output.
You can use regex withour checking the square brackets.
This could work:
String strPattern = "(.+)(\\[.+\\])";
Pattern p = Pattern.compile(strPattern);
Matcher m = p.matcher("<yout string>");
if (m.matches()){
<your code here>
}

IndexOutOfBoundsException when using Matcher.find()

This Java program showing me IndexOutOfBoundsException when it tries to invoke group(1). If I replace 1 with 0 then the whole line is printed.. What do I have to do?
Pattern pattern = Pattern.compile("<abhi> abhinesh </abhi>");
Matcher matcher = pattern.matcher("<abhi> abhinesh </abhi>");
if (matcher.find())
System.out.println(matcher.group(1));
else
System.out.println("Not found");
index starts at 0 so use matcher.group(0)
Edit : To match the text between tag use this regex <abhi>(.*)<\\/abhi>
This post may shed more light on your question.
Confused about Matcher Group.
In short you haven't defined any regular expression grouping to reference an alternate group. You only have the full matching string.
Below if you try adding a grouped regular expression to parse the xml you'll notice 0 has the full string, 1 has the begin tag, 2 has the value, and 3 has the end tag.
Pattern pattern = Pattern.compile("<([a-z]+)>([a-z ]+)</([a-z]+)>");
Matcher matcher = pattern.matcher("<abhi> abhinesh </abhi>");
if (matcher.find()){
System.out.println(matcher.group(0));//<abhi> abhinesh </abhi>
System.out.println(matcher.group(1));//abhi
System.out.println(matcher.group(2));// abhinesh
System.out.println(matcher.group(3));//abhi
}else{
System.out.println("Not found");
}
Try this this regex:
<abhi>(.*)<\\/abhi>
The text you're after will be stored in the first capture group.
Example:
String regex = "<abhi>(.*)<\\/abhi>";
String input = "<abhi>foo</abhi>";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
if (m.find()) {
System.out.println(m.group(1));
}

I want to extracting css image path by Java Pattern expression

All , I want to write a pattern regex to extract the: "/images/colorbox/ie6/borderBottomRight.png" from cssContent=".cboxIE6 #cboxBottomRight{background:url(../images/colorbox/ie6/borderBottomRight.png);}"
Who can write a pattern regex for me? Thanks a lot.
My regex can't work as:
Pattern pattern = Pattern.compile("[.*]*/:url/(/././/(.+?)/)/;[.*]*");
Matcher matcher = pattern.matcher(cssContent);
if(matcher.find()){
System.out.println(matcher.group(0));
}
Pattern pattern = Pattern.compile(":url\\(\\.\\.([^)]+)\\)");
Matcher matcher = pattern.matcher(cssContent);
if(matcher.find()){
System.out.println(matcher.group(1));
}
The regex used to match is (quoted and without \ escaped)
":url\(\.\.([^)]+)\)"
which looks for :url(.. followed by [^)] anything that's not a closing ) bracket + one or more times; finally followed by the closing ) bracket. The group () captured is available at group(1) whereas group(0) would give you the complete string that matched i.e. from :url to the closing ).
The biggest error you were making was using "/" to escape your literal characters. You need to use "\", and annoyingly, in a java string "\" must be escaped with "\", so the total escape sequence is "\\". Then, you have matcher.group(0), which matches the entire pattern. You needmatcher.group(1)` to match the first (and only) group in your regex, which contains your string of interest. Here's the corrected code:
String cssContent = "cssContent=\".cboxIE6 #cboxBottomRight{background:url(../images/colorbox/ie6/borderBottomRight.png);}\"";
String regex = ".*?:url\\(\\.\\.(.+?)\\);[.*]*";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(cssContent);
if(matcher.find()){
System.out.println(matcher.group(1));
}

Categories

Resources