No match found in simple regex

Given a token in the format “word_suffix”, I want to match and capture the “suffix” part.

For instance, in “Peter_NNP” I want to capture “NNP”. I wrote:

        String p="Peter_NNP";
        Matcher matcher=Pattern.compile(".+_(.*s)").matcher(p);
        System.out.println(matcher.group(1));

instead of printing “NNP” as I would expect, it arises the following exception:

Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Unknown Source)

Note that “word” and “suffix” part can be made of any character.


Source: regex

Leave a Reply