• 0

[JAVA] Regex Matching problem


Question

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

  • 0

.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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.