Whitespace in Java's regular expression

送分小仙女□ 提交于 2019-12-11 19:46:56

问题


I'm trying to write a regular expression to mach an IRC PRIVMSG string. It is something like:

:nick!name@some.host.com PRIVMSG #channel :message body

So i wrote the following code:

Pattern pattern = Pattern.compile("^:.*\\sPRIVMSG\\s#.*\\s:");
Matcher matcher = pattern.matcher(msg);

if(matcher.matches()) {
    System.out.println(msg);
}

It does not work. I got no matches. When I test the regular expression using online javascript testers, I got matches.

I tried to find the reason, why it doesn't work and I found that there's something wrong with the whitespace symbol. The following pattern will give me some matches:

Pattern.compile("^:.*");

But the pattern with \s will not:

Pattern.compile("^:.*\\s");

It's confusing.


回答1:


The java matches method strikes again! That method only returns true if the entire string matches the input. You didn't include anything that captures the message body after the second colon, so the entire string is not a match. It works in testers because 'normal' regex is a 'match' if any part of the input matches.

Pattern pattern = Pattern.compile("^:.*?\\sPRIVMSG\\s#.*?\\s:.*$");

Should match




回答2:


If you look at the documentation for matches(), uou will notice that it is trying to match the entire string. You need to fix your regexp or use find() to iterate through the substring matches.



来源:https://stackoverflow.com/questions/10521521/whitespace-in-javas-regular-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!