简体   繁体   English

Java Matcher Pattern问题

[英]Java Matcher Pattern issue

I am trying to extract everything that is after this string path /share/attachments/docs/ . 我正在尝试提取此字符串路径/share/attachments/docs/ All my strings are starting with /share/attachments/docs/ 我所有的字符串都以/share/attachments/docs/开头

For example: /share/attachments/docs/image2.png Number of characters after ../docs/ is not static! 例如: /share/attachments/docs/image2.png之后的字符数不是静态的!

I tried with 我尝试过

   Pattern p = Pattern.compile("^(.*)/share/attachments/docs/(\\d+)$");
   Matcher m = p.matcher("/share/attachments/docs/image2.png");
   m.find();         
   String link = m.group(2);    
   System.out.println("Link #: "+link);

But I am getting Exception that: No match found. 但是我收到异常消息: No match found. Strange because if I use this: 奇怪,因为如果我用这个:

   Pattern p = Pattern.compile("^(.*)ABC Results for draw no (\\d+)$");
   Matcher m = p.matcher("ABC Results for draw no 2888");

then it works!!! 然后就可以了!!!

Also one thing is that in some very rare cases my string does not start with /share/attachments/docs/ and then I should not parse anything but that is not related directly to the issue, but it will be good to handle. 还有一件事是,在极少数情况下,我的字符串不是以/share/attachments/docs/开头,然后我不应该解析任何东西,但这与问题没有直接关系,但是处理起来会很好。

I am getting Exception that: No match found. 我收到以下异常: No match found.

This is because image2.png doesn't match with \\d+ use a more appropriate pattern like .+ assuming that you want to extract image2.png . 这是因为image2.png\\d+不匹配,因此使用更合适的模式(如.+假设您要提取image2.png

Your regular expression will then be ^(.*)/share/attachments/docs/(.+)$ 这样,您的正则表达式将为^(.*)/share/attachments/docs/(.+)$


In case of ABC Results for draw no 2888 , the regexp ^(.*)ABC Results for draw no (\\\\d+)$ works because you have several successive digits at the end of your String while in the first case you had image2.png that is a mix of letters and digits which is the reason why there were no match found. ABC Results for draw no 2888 ^(.*)ABC Results for draw no (\\\\d+)$的regexp ^(.*)ABC Results for draw no (\\\\d+)$有效,因为在String的末尾有几个连续的数字,而在第一种情况下,您有image2.png包含字母和数字的image2.png ,这就是找不到匹配项的原因。


Generally speaking to avoid getting an IllegalStateException: No match found , you need first to check the result of find() , if it returns true the input String matches: 一般而言,为避免得到IllegalStateException: No match found ,您需要首先检查find()的结果,如果它返回true则输入String匹配:

if (m.find()) {
   // The String matches with the pattern
   String link = m.group(2);    
   System.out.println("Draw #: "+link);
}  else {
   System.out.println("Input value doesn't match with the pattern");
}

The regular expression \\d+ (expressed as \\\\d+ inside a string literal) matches a run of one or more digits . 正则表达式\\d+ (在字符串文字中表示为\\\\d+ )匹配一个或多个数字 Your example input does not have a corresponding digit run, so it is not matched. 您的示例输入没有对应的数字运行,因此不匹配。 The regex metacharacter . 正则表达式元字符. matches any character (+/- newline, depending on regex options); 匹配任何字符(+/-换行符,取决于正则表达式选项); it seems like that may be what you're really after. 看来这可能正是您真正追求的。

Additionally, when you use Matcher.find() it is unnecessary for the pattern to match the whole string, so it is needless to include .* to match leading context. 另外,当您使用Matcher.find()时,模式不需要匹配整个字符串,因此无需包含.*即可匹配前导上下文。 Furthermore, find() returns a value that tells you whether a match to the pattern was found. 此外, find() 返回一个值该值告诉您是否找到与模式匹配的内容。 You generally want to use this return value, and in your particular case you can use it to reject those rare non-matching strings. 通常,您希望使用此返回值,在特定情况下,您可以使用它来拒绝那些罕见的不匹配字符串。

Maybe this is more what you want: 也许这更是您想要的:

Pattern p = Pattern.compile("/share/attachments/docs/(.+)$");
Matcher m = p.matcher("/share/attachments/docs/image2.png");
String link;

if (m.find()) {
    link = m.group(1);
    System.out.println("Draw #: " + link);
} else {
    link = null;
    System.out.println("Draw #: (not found)");
}

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

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