?
-
I'm having trouble parsing a string with the following formats using
regular expression:
Format 1:
String string1 = "refix COMMAND param1 param2";
System.out.println(m.group(0)); ----> refix
System.out.println(m.group(1)); ----> COMMAND
System.out.println(m.group(2)); ----> param1
System.out.println(m.group(3)); ----> param2
Format 2:
String string2 = "COMMAND param1 param2";
System.out.println(m.group(0)); ----> null
System.out.println(m.group(1)); ----> COMMAND
System.out.println(m.group(2)); ----> param1
System.out.println(m.group(3)); ----> param2
This is the pattern i'm using, which obviously isn't right:
Pattern p =
Pattern.compile("\\S*?\\s*?)((\\S*?\\s*?)(\\s*?\\S*?\\s*?))");
Matcher m = p.matcher(string1);
m.matches();
regular expression:
Format 1:
String string1 = "refix COMMAND param1 param2";
System.out.println(m.group(0)); ----> refix
System.out.println(m.group(1)); ----> COMMAND
System.out.println(m.group(2)); ----> param1
System.out.println(m.group(3)); ----> param2
Format 2:
String string2 = "COMMAND param1 param2";
System.out.println(m.group(0)); ----> null
System.out.println(m.group(1)); ----> COMMAND
System.out.println(m.group(2)); ----> param1
System.out.println(m.group(3)); ----> param2
This is the pattern i'm using, which obviously isn't right:
Pattern p =
Pattern.compile("\\S*?\\s*?)((\\S*?\\s*?)(\\s*?\\S*?\\s*?))");
Matcher m = p.matcher(string1);
m.matches();