Firstmate Posted May 29, 2009 Share Posted May 29, 2009 Yes this is part of a pjirc bot, but I have this code: if (cmd.equals("`")) { try { String pattern = "(sw|sp|fl){1}"; Pattern z = Pattern.compile(pattern); boolean kick = z.matcher(data[2]).lookingAt(); this.sendMessage(channel,"Checking syntax..."); if (data[1].equals("") || data[1].equals(null) && kick) { this.sendMessage(channel, "Invalid Syntax. ` <user> <reason>"); return; } } catch (ArrayIndexOutOfBoundsException e) { this.sendMessage(channel, "Invalid Syntax. ` <user> <reason>"); return; } Sample input: ` test sw 1 So I have 2 Questions. 1.) In the .matcher method, how do you put more than one. e.x .matcher(data[2] data[3]) 2.) The regex fails to catch sww as an invalid syntax. If possible can someone fix the code to allow 1) into the regex and fix it as well? An explanation of the problem would be nice too :D Link to comment Share on other sites More sharing options...
0 Knad Posted May 30, 2009 Share Posted May 30, 2009 Not sure about '1' but you could check that 'sw' is followed by a space (also preceded if you need) String pattern = "(sw\ |sp\ |fl\ ){1}"; Also, matcher can only take one input: http://java.sun.com/j2se/1.4.2/docs/api/ja...g.CharSequence) So either use separate matchers, or concatenate inputs depending on what you need to do. Link to comment Share on other sites More sharing options...
0 +M2Ys4U Subscriber¹ Posted June 1, 2009 Subscriber¹ Share Posted June 1, 2009 .lookingAt() "Returns: true if, and only if, a prefix of the input sequence matches this matcher's pattern" (emphasis mine) It will fail to match if there's anything (e.g. a space) before it. Link to comment Share on other sites More sharing options...
0 Firstmate Posted June 3, 2009 Author Share Posted June 3, 2009 Somewhat of a bump, but what is considered a prefix? Link to comment Share on other sites More sharing options...
Question
Firstmate
Yes this is part of a pjirc bot, but I have this code:
if (cmd.equals("`")) { try { String pattern = "(sw|sp|fl){1}"; Pattern z = Pattern.compile(pattern); boolean kick = z.matcher(data[2]).lookingAt(); this.sendMessage(channel,"Checking syntax..."); if (data[1].equals("") || data[1].equals(null) && kick) { this.sendMessage(channel, "Invalid Syntax. ` <user> <reason>"); return; } } catch (ArrayIndexOutOfBoundsException e) { this.sendMessage(channel, "Invalid Syntax. ` <user> <reason>"); return; }Sample input:
` test sw 1
So I have 2 Questions.
1.) In the .matcher method, how do you put more than one. e.x .matcher(data[2] data[3])
2.) The regex fails to catch sww as an invalid syntax. If possible can someone fix the code to allow 1) into the regex and fix it as well?
An explanation of the problem would be nice too :D
Link to comment
Share on other sites
3 answers to this question
Recommended Posts