简体   繁体   中英

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.

You need to call find() to grab your match group. Also, your capture group expects that there should be whitespace at the end of the string, in "Peter_NNP" there is none, .* is enough here.

String s  = "Peter_NNP";
Pattern p = Pattern.compile(".+_(.*)");
Matcher m = p.matcher(s);
if (m.find()) {
  System.out.println(m.group(1)); //=> "NNP"
}

But, I would think a simple split would be fine here:

String s = "Peter_NNP";
String[] parts = s.split("_");
System.out.println(parts[1]);    //=> "NNP"

Just to add on hwnd answer, If you want to capture anything after first underscore(even if there is no character before underscore). Thanks hwnd for making me understand this.

            String s="_NNP";
            Matcher matcher=Pattern.compile(".*?_(.*)").matcher(s);
            while (matcher.find()) {
                System.out.println(matcher.group(1));
              }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM