0

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.

dotintegral
  • 918
  • 1
  • 10
  • 23

2 Answers2

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

Affe
  • 47,174
  • 11
  • 83
  • 83
0

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.

user845279
  • 2,794
  • 1
  • 20
  • 38