"No Match Found" When Using Matcher's Group Method

No match Found when using matcher's group method

pattern.matcher(input) always creates a new matcher, so you'd need to call matches() again.

Try:

Matcher m = responseCodePattern.matcher(firstHeader);
m.matches();
m.groupCount();
m.group(0); //must call matches() first
...

Java RegEx no match found error

No match has been attempted. Call find() before calling group().

public static void main(String[] args) {
String requeststring = "POST //upload/sendData.htm HTTP/1.1";
String requestpattern = "^[A-Za-z]+ \\/+(\\w+)";
Pattern p = Pattern.compile(requestpattern);
Matcher matcher = p.matcher(requeststring);
System.out.println(matcher.find());
System.out.println(matcher.group(1));
}

Output:

true
upload

IllegalStateException: No match found when getting match group

Problem is this line:

streetAddress = streetAddressMatch.group();

You are calling group() without calling streetAddress.find() or streetAddress.matches() before.

Java regex, IllegalStateException: No match found

Let's break down your pattern:

"#^(.+)#"
  • #^ - Literal string
  • (.+) - Any character (may or may not match line terminators) 1 or more times
  • # - Literal string

So why does sel=45 fail to match? The String does not begin with "#^" and does not end with "#".

It would seem you want to capture data in between "#" that may or may not be there (You need to clarify this). The following pattern accomplishes that:

"#?([^#]+)#?"

This will match sel=45 and will match #sel=45#

Example:

public static void main(String[] args) throws Exception {
String regulex = "#?([^#]+)#?"; //even this guy returns no matches

System.out.println(checkRegex("#sel=45#", regulex));
System.out.println(checkRegex("sel=45", regulex));
}

public static String checkRegex(String check, String regex) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(check);
return m.matches() ? m.group(1): null;
}

Results:

sel=45
sel=45

Matcher find a pattern but throw No match available on `start` method

Problem is that you're using pattern.matcher(text) again and that creates another Matcher instance and when you call start() on a new instance it throws exception since find or matches or lookingAt has not been called before.

You may use it like this:

String text = "Hello bob, remind me to do a lot of things today";
final Pattern pattern = Pattern.compile("remind.*\\hto\\h");
Matcher m = pattern.matcher(text); // create it only once
// Looking for "remind <anyWord> to "
if (m.find()) {
System.err.println( "Start: " + m.start() + ", match: " + m.group() );
}

Also note changes in your regex. \h matches a horizontal whitespace whereas .to. will match any character before and after to hence your regex will match:

"remind me to do a lot of things tod"

instead of intended:

"remind me to do " 

Matcher: No match found...

Your problem is that you never call find before trying to get group, that is why you don't have match.

You need this line of code:

m.find();

Before you do

String fromMatcher = m.group(0); //first word surrounded by hash, without the hash

But even then, you will get it all with hash tags around, to avoid that you should create group only for inner text around hash tags like this:

 Pattern ptrn = Pattern.compile("#([^#]+)#");

And when you are accessing your group it will be group number 1 (because 0 is whole pattern). So change getting group like this:

String fromMatcher = m.group(1); //first word surrounded by hash, without the hash

Regex matcher - No match found

Here is an exaple of how to get all the values you need with find():

String tring = "CHARDATA_FRMT: <<<$gen>>>(((valu e)))    <<<$gen>>>(((value 13231)))\n<<<$gen>>>(((value 13231)))";
Pattern p = Pattern.compile("<{3}\\$([\\w ]+)>{3}\\s?\\({3}([\\w ]+)\\){3}");
Matcher m = p.matcher(tring);
while (m.find()){
System.out.println("Gen: " + m.group(1) + ", and value: " + m.group(2));
}

See IDEONE demo

Note that you do not have to escape < and > in Java regex.

RegEx Pattern not matching in JAVA

You need to call Matcher#find() to actually get a match:

Pattern p = Pattern.compile("([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)");
Matcher matcher = p.matcher("1-1-3-1-4");
if (matcher.find()) {
System.out.println(matcher.group(0))
}

If you were expecting multiple matches, you could use a while loop instead of an if statement.

Also note that you actually have five capture groups in your patterns. Capture groups are denoted by placing a portion of the pattern in parentheses. If you don't intend/need to capture the five separated numbers in your pattern individually, then you can consider telling the regex engine not to capture them, e.g. use this:

Pattern p = Pattern.compile("(?:[0-9]+)-(?:[0-9]+)-(?:[0-9]+)-(?:[0-9]+)-(?:[0-9]+)");

Demo



Related Topics



Leave a reply



Submit