FireLight
FireLight

Reputation: 325

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
*/

Upvotes: 1

Views: 358

Answers (2)

Eugene
Eugene

Reputation: 11075

Use m.find() instead of m.lookingAt()

You can print what you get by m.group()

Please check code below.

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.find();
        System.out.println("Match was found: " + matchFound);
        System.out.println("Matched string is: " + m.group());
    }
}

The javadoc of lookingAt() is

public boolean lookingAt()

Attempts to match the input sequence, starting at the beginning of the region, against the pattern. Like the matches method, this method always starts at the beginning of the region; unlike that method, it does not require that the entire region be matched.

If the match succeeds then more information can be obtained via the start, end, and group methods.

Returns:true if, and only if, a prefix of the input sequence matches this matcher's pattern

That means, this method expects the regex matches at the very beginning of input String.

This method is not used frequently, the effect is like you modify your regex to "^n.w\\s+h(a|i)s", and use find() method. It also puts a limitation that the regex matches at the very beginning of the input String.

Upvotes: 1

Raghav
Raghav

Reputation: 4638

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.

Upvotes: 2

Related Questions