RNGesus RNGesus - 21 days ago 11
Java Question

Very simple Java regex not giving expected result

Today is my first day learning regular expressions (literally no background before this) through the chapter Strings in the book Thinking in Java 4th Edition. I am pulling my hair out as to why the regular expression is not matching any region of the input string. I have tested this in regex101 and I get the result I expected, but in Java (which you can't test on the regex101 site admittedly) the result is different.

EDIT: Doing exercise 10 in the chapter

Regex: n.w\s+h(a|i)s

Input String: Java now has regular expressions

Expected Result: A match found in the region "now has" of the input string

Actual Result: No match found

My relevant code:

import java.util.regex.*;

public class Foo {
public static void main(String[] args) {
// NOTE: I've also tested passing the regex as an arg from the command line
// as "n.w\s+h(a|i)s"
String regex = "n.w\\s+h(a|i)s";
String input = "Java now has regular expressions";

Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);

// Starting at the beginning of the input string, look for a match in ANY
// region of the input string
boolean matchFound = m.lookingAt();
System.out.println("Match was found: " + matchFound);
}
}
/* OUTPUT
-> Match was found: false
*/

Answer

Use boolean matchFound = m.find(); instead of boolean matchFound = m.lookingAt();

From Javadocs

lookingAt() tries to match the input sequence, starting at the beginning of the region, against the pattern.

Comments