Java is not able to find a match for regex [duplicate] - java

This question already has answers here:
Rationale for Matcher throwing IllegalStateException when no 'matching' method is called
(6 answers)
Closed 3 years ago.
I have been trying to figure out why java is not able to find nay matches for this regex pattern, I tested it with https://regexr.com/ and it works fine.
But when I escape it and use it in my class it complains about no match found.
Regex pattern I used in regexr.com
pattern: TEST\(":"\)\/(\w+)\[#name="(.*)"\]
Strings to test: TEST(":")/Role[#name="TestRole"]
The website is able to find two groups: 1. Role and 2. TestRole. And thats the required behaviour.
But when I use it in my code my test returns with no match found, here are the escaped pattern and test strings:
public String extractCorrectExp() {
String path1 = "TEST(\":\")/Role[#name=\"TetsRole\"]";
String pattern = "TEST\\(\":\"\\)\\/(\\w+)\\[#name=\"(.*)\"\\]";
Pattern pat = Pattern.compile(pattern);
Matcher match = pat.matcher(path1);
return match.group();
}
It returns:
java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:536)
at java.util.regex.Matcher.group(Matcher.java:496)
Could this be escaping issue? or somehow I am using regex identifiers which are not compatible with java?

find() must be called on the Matcher before group() can be called. Your code will work if you change it to:
String path1 = "TEST(\":\")/Role[#name=\"TestRole\"]";
String pattern = "TEST\\(\":\"\\)/(\\w+)\\[#name=\"(.*)\"]";
Pattern pat = Pattern.compile(pattern);
Matcher match = pat.matcher(path1);
if ( match.find() ) {
System.out.println(match.group());
}
If condition can be used for a single match, and while loop can be used for multiple matches.

Related

Java regex issue with: ###tag1###value1###tag2###value2###tag3###value3###

I'm having trouble writing a regex to validate the following pattern:
###tag1###value1###tag2###value2###tag3###value3###
This can repeat indefinitely
The tags and values can contain any printable char as well as whitespace and newlines
I've been trying variations of this:
String pattern = "(###[\\p{Print}\\s]+###[\\p{Print}\\s]+)+###"
But it is not enforcing the pattern for certain cases such as:
###testTag###testValue###testtag2testvalue2### (missing the ### for tag2/value2)
And its invalidating some valid inputs such as:
###component###CORE COMMAND GROUP###severity###DEBUG###message###Validating potential model load directory: C:/some/dir/market-xxxxxx/market-xxxxxx-PRODUCT-kit/data/XXX_7020190724201513_0X###
Any suggestions on how I can improve this regex? Thanks!
I'm using the Java pattern lib.
You ma use this regex"
final String regex =
"^(?:###(?:(?!###|###)\\P{C})+###(?:(?!###|###)\\P{C})+)+###$";
RegEx Demo
\P{C} matches any printable unicode character
(?:(?!###|###)\\P{C}) matches any printable unicode character if it is not immediately followed by ### or ###
This works well :
String s1 = "###tag1###value1###tag2###value2###tag3###value3###";
String s2 = "testTag###testValue###testtag2testvalue2###";
String s3 = "component###CORE COMMAND GROUP###severity###DEBUG###message###Validating potential model load directory: C:/some/dir/market-xxxxxx/market-xxxxxx-PRODUCT-kit/data/XXX_7020190724201513_0X###";
String p = "(###|^)?(((\P{C})+?###(\P{C})+?)###|$)+?";
Matcher m1 = p.matcher(s1); // testTag###testValue
Matcher m2 = p.matcher(s2); // tag1###value1, tag2###value2, tag3###value3
Matcher m3 = p.matcher(s3); // component###CORE COMMAND GROUP, severity###DEBUG
This will almost get you there:
(###)((.+?)(###)(.+?)(###))
https://regex101.com/r/5rfpL0/4
I'm just having a hard time figuring out why:
(###)((.+?)(###)(.+?)(###))+
doesn't make it behave as expected :-/

Extract numbers from String using Regex [duplicate]

This question already has answers here:
Matching one string multiple times using regex in Java
(4 answers)
Closed 4 years ago.
I have a string such as below:
String str="tile tile-2 tile-position-1-4"
I wish to receive the numbers in an array,such as [2,1,4].
My own solution is to break the string using split, but i am wondering if there is a shortcut using Regx
Thanks to #nitzien with Regx it tried:
String pattern= "^tile tile-(\\d*) tile-position-(\\d*)-(\\d*)$";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
System.out.println(m.group(0));
System.out.println(m.group(1));
System.out.println(m.group(2));
But, it complains with:
java.lang.IllegalStateException: No match found
regex pattern - "^tile tile-(\d*) tile-position-(\d*)-(\d*)$"
replacement - "[\1,\2,\3]"
Replacement is string which you will need to convert to array depending on which language you are using.
Updated answer after edited question
String str="tile tile-2 tile-position-1-4";
String pattern= "^tile tile-(\\d*) tile-position-(\\d*)-(\\d*)$";
System.out.println(str.replaceAll(pattern, "$1,$2,$3"));
This will give
2,1,4

Possible backslash escaping issue when trying to perform a regex

Hi I have the following sentence which is within a much larger string variable:
New Component <b>TEST</b> is successfully registered.
I'm trying to perform a regex match to find this sentence within the string. The word TEST is variable and can be any word.
I'm using the following pattern in regexr which runs fine:
New Component <b>\w*<\/b> is successfully registered.
In my java code I have to write it as
Pattern p = Pattern.compile("New Component <b>\\w*<\\/b> is successfully registered.");
Matcher m = p.matcher(result.toString());
if (m.matches()) {
System.out.println("hurray!");
}
This is because I need to escape the backslashes. However the pattern is not receiving a match in the php code and hurray is not printed. Is there an issue with the backslashes or the way I have used them here that is causing the matcher to fail?
Try adding .* to the start and end of the pattern:
Pattern p = Pattern.compile(".*New Component <b>\\w*<\\/b> is successfully registered\..*");
Your pattern is trying to match the string, however it won't match as it is part of a larger string, so any characters before or after the target string will not be accepted by the regular expression and cause it to fail.
.* tells the matcher to accept 0 or more of ANY character before and after your target string.
Edit: Also if you want to match the fullstop at the end of the line, you should escape the fullstop with \., this is because the dot has a special meaning in regex, it means any character.
Further to #dahui's answer, the other option is to switch m.matches() with m.find().
.matches() requires the regex to match the entire string.
.find() required the regex to match any substring of the string.
Edit:
Running the following does print "hurray!" when I run it:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SO {
public static void main(String[] args) {
Pattern p = Pattern.compile("New Component <b>\\w*<\\/b> is successfully registered.");
Matcher m = p.matcher("New Component <b>TEST</b> is successfully registered.");
if (m.matches()) {
System.out.println("hurray!");
}
}
}
Is it possible result.toString() isn't what you think it is?

Regext do not work in java but it does inr egex check [duplicate]

I have a piece of code that I can't make it working on Eclipse with Java 1.7 installed.
There is a regex expression I want to use to match and extract 2 strings from every match, so I am using 2 groups for this.
I have tested my expression in many websites (online regex testers) and it works for them bust it isn't working on my Java project in Eclipse.
The source string looks like anyone of these:
Formal Language: isNatural
Annotation Tool: isHuman%Human Annotator: isHuman
Hybrid Annotation: conceptType%Hybrid Annotation Tool: conceptType%Hybrid Tagset: conceptType
... and so on.
I want to extract the first words before the ":" and the word after for every match.
The regex I'm using is this:
(\w*\s*\w+):(\s+\w+)%{0,1}
And the snippet of code:
String attribute = parts[0];
Pattern pattern = Pattern.compile("(\\w*\\s*\\w+):(\\s+\\w+)%{0,1}");
Matcher matcher = pattern.matcher(attribute);
OWLDataProperty dataProp = null;
if (matcher.matches()){
while (matcher.find()){
String name = null, domain = null;
domain = matcher.group(1);
name = matcher.group(2);
dataProp = factory.getOWLDataProperty(":"+Introspector.decapitalize(name), pm);
OWLClass domainClass = factory.getOWLClass(":"+domain.replaceAll(" ", ""), pm);
OWLDataPropertyDomainAxiom domainAxiom = factory.getOWLDataPropertyDomainAxiom(dataProp, domainClass);
manager.applyChange(new AddAxiom(ontology, domainAxiom));
}
Does anybody of you know why it's not working?
Many thanks.
When using matches(), you are asking if the string you provided matches your regex as a whole. It is as if you added ^ at the beginning of your regex and $ at the end.
Your regex is otherwise fine, and returns what you expect. I recommend testing it regexplanet.com, Java mode. You will see when matches() is true, when it false, and what each find() will return.
To solve your problem, I think you only need to remove the if (matcher.matches()) condition.

Java Regex pattern that matches in any online tester but doesn't in Eclipse

I have a piece of code that I can't make it working on Eclipse with Java 1.7 installed.
There is a regex expression I want to use to match and extract 2 strings from every match, so I am using 2 groups for this.
I have tested my expression in many websites (online regex testers) and it works for them bust it isn't working on my Java project in Eclipse.
The source string looks like anyone of these:
Formal Language: isNatural
Annotation Tool: isHuman%Human Annotator: isHuman
Hybrid Annotation: conceptType%Hybrid Annotation Tool: conceptType%Hybrid Tagset: conceptType
... and so on.
I want to extract the first words before the ":" and the word after for every match.
The regex I'm using is this:
(\w*\s*\w+):(\s+\w+)%{0,1}
And the snippet of code:
String attribute = parts[0];
Pattern pattern = Pattern.compile("(\\w*\\s*\\w+):(\\s+\\w+)%{0,1}");
Matcher matcher = pattern.matcher(attribute);
OWLDataProperty dataProp = null;
if (matcher.matches()){
while (matcher.find()){
String name = null, domain = null;
domain = matcher.group(1);
name = matcher.group(2);
dataProp = factory.getOWLDataProperty(":"+Introspector.decapitalize(name), pm);
OWLClass domainClass = factory.getOWLClass(":"+domain.replaceAll(" ", ""), pm);
OWLDataPropertyDomainAxiom domainAxiom = factory.getOWLDataPropertyDomainAxiom(dataProp, domainClass);
manager.applyChange(new AddAxiom(ontology, domainAxiom));
}
Does anybody of you know why it's not working?
Many thanks.
When using matches(), you are asking if the string you provided matches your regex as a whole. It is as if you added ^ at the beginning of your regex and $ at the end.
Your regex is otherwise fine, and returns what you expect. I recommend testing it regexplanet.com, Java mode. You will see when matches() is true, when it false, and what each find() will return.
To solve your problem, I think you only need to remove the if (matcher.matches()) condition.

Categories