Reputation: 938
I'm trying to write a regular expression to mach an IRC PRIVMSG string. It is something like:
:[email protected] 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.
Upvotes: 0
Views: 492
Reputation: 2804
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.
Upvotes: 0
Reputation: 47984
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
Upvotes: 2