简体   繁体   English

没有匹配在简单的regex发现

[英]No match found in simple regex

Given a token in the format "word_suffix", I want to match and capture the "suffix" part. 给定一个格式为“ word_suffix”的令牌,我想匹配并捕获“后缀”部分。

For instance, in "Peter_NNP" I want to capture "NNP". 例如,在“ Peter_NNP”中,我要捕获“ 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: 而不是像我期望的那样打印“ NNP”,它出现以下异常:

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. 你需要调用find()来抓住你的比赛组。 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. 此外,您的采集集团预计应该在字符串的结尾空白,在“Peter_NNP”没有, .*是不够这里。

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). 只是要添加hwnd答案,如果您想在第一个下划线之后捕获任何内容(即使下划线之前没有字符)。 Thanks hwnd for making me understand this. 谢谢hwnd使我明白这一点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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