在以下情况下需要Java regex模式:
情况1:
输入字符串:
"a"
火柴:
a
情况2:
输入字符串:
"a b"
火柴:
a b
情况3:
输入字符串:
"aA Bb" cCc 123 4 5 6 7xy "\"z9" "\"z9$^"
火柴:
aA Bb
cCc
123
4
5
6
7xy
"z9
"z9$^
情况4:
输入字符串:
"a b c
火柴:
None - since the quotes are unbalanced, hence pattern match fails.
情况5:
输入字符串:
"a b" "c
火柴:
None - since the quotes are unbalanced, hence pattern match fails.
情况6:
输入字符串:
"a b" p q r "x y z"
火柴:
a b
p
q
r
x y z
情况7:
输入字符串:
"a b" p q r "x y \"z\""
火柴:
a b
p
q
r
x y "z"
情况8:
输入字符串:
"a b" p q r "x \"y \"z\""
火柴:
a b
p
q
r
x "y "z"
当然,最简单的一个是:
情况9:
输入字符串:
a b
火柴:
a
b
使用模式尝试过,但似乎与上述所有情况都不匹配.
public List<String> parseArgs(String argStr) {
List<String> params = new ArrayList<String>();
String pattern = "\\s*(\"[^\"]+\"|[^\\s\"]+)";
Pattern quotedParamPattern = Pattern.compile(pattern);
Matcher matcher = quotedParamPattern.matcher(argStr);
while (matcher.find()) {
String param = matcher.group();
System.out.println(param);
params.add(param);
}
return params;
}
public void test(String argStr) {
String[] testStrings = new String[]{"a", "a b", "a b \"c\"", "a b \"c"};
for(String s: testStrings){
parseArgs(s);
}
}
解决方法:
已经编写了一个类“ CLIParser”,它将为您提供结果.
//instantiate the CLIParser
CLIParser parser = new CLIParser("\"a b\" p q r \"x y z\"");
//call the method getTokens which gives you the result.
ArrayList<String> resultTokens = parser.getTokens();
###################CLI Parser Class definition#################################
class CLIParser {
private String cmdString;
public CLIParser(String cmdString) {
this.cmdString = cmdString;
}
public ArrayList<String> getTokens() throws Exception {
ArrayList<String> finalTokens = new ArrayList<String>();
ArrayList<StringBuffer> tokens = new ArrayList<StringBuffer>();
char inArray[] = this.cmdString.toCharArray();
StringBuffer token = new StringBuffer();
int valid = checkIfTheStringIsValid(inArray);
if (valid == -1) {
for (int i = 0; i <= inArray.length; i++) {
if (i != inArray.length) {
if ((inArray[i] != ' ') && (inArray[i] != '"')) {
token.append(inArray[i]);
}
if ((inArray[i] == '"') && (inArray[i - 1] != '\\')) {
i = i + 1;
while (checkIfLastQuote(inArray, i)) {
token.append(inArray[i]);
i++;
}
}
}
if (i == inArray.length) {
tokens.add(token);
token = new StringBuffer();
} else if (inArray[i] == ' ' && inArray[i] != '"') {
tokens.add(token);
token = new StringBuffer();
}
}
} else {
throw new InvalidCommandException(
"Invalid command. Couldn't identify sequence at position "
+ valid);
}
for(StringBuffer tok:tokens){
finalTokens.add(tok.toString());
}
return finalTokens;
}
private static int checkIfTheStringIsValid(char[] inArray) {
Stack myStack = new Stack<Character>();
int pos = 0;
for (int i = 0; i < inArray.length; i++) {
if (inArray[i] == '"' && inArray[i - 1] != '\\') {
pos = i;
if (myStack.isEmpty())
myStack.push(inArray[i]);
else
myStack.pop();
}
}
if (myStack.isEmpty())
return -1;
else
return pos;
}
private static boolean checkIfLastQuote(char inArray[], int i) {
if (inArray[i] == '"') {
if (inArray[i - 1] == '\\') {
return true;
} else
return false;
} else
return true;
}
}