I’m trying to get matches for commands like this;
[AUTR| <version_software> | <version_protocol> | <msg> ]
[PING]
What is the regular expression that find this matches for the first command?
AUTR
version_software
version_protocol
msg
this is the code that parse that:
String[] tokens = msg.replace('<',' ').replace('>',' ').replace('[', ' ').replace(']', ' ').split("\\|");
for (int i=0; i<tokens.length; i++) tokens[i] = tokens[i].trim();
I’m only wondering how it can be done with a regex solution.
EDIT:
I’m trying to match groups with easier expressions, and with this code the call to m.groupCount returns one… but when I try to print it… it throws this exception “java.lang.IllegalStateException: No match found”
Pattern pattern = Pattern.compile("([\\w+])");
Matcher m = pattern.matcher("[AUTR]");
for (int i=0; i<m.groupCount();i++)
{
System.out.println(m.group(i));
}
EDIT:
http://fiddle.re/6ykc
Regular Expression:
Java Regex String:
Note that this is for variable commands now and that all extra parameters must match the following character set [a-zA-Z_0-9. ] (Includes periods and spaces).
Issue: There is an issue with variable length commands that you cannot capture more than one group with a variable type grouping.
EDIT 2:
In order to get all of them you can do 2 regular expressions, one to grab the command:
And find that and then find the parameters which you can use the <> as your key character to select:
Hope that helps.
ORIGINAL:
Not exactly sure on the formatting, are the “<” and “>” and “|” required? And what are the formats for the command, version_software, version_protocol and message? This is my attempt though for regular expressions (tested in Python)
You need to make sure to escape the brackets and the pipe symbols (I added \s* conditions between because I don’t know if there will be spaces or not. If you do:
It should give all tokens in python at least. I left it more hardcoded to allow room for adjustments on each token you wanted to grab, otherwise you could reduce the last 3 parts by making it a group and saying to repeat 3 times. Let me know results?