【发布时间】:2014-09-17 15:36:55
【问题描述】:
我一直在尝试用 Java 中的正则表达式解析命令,但没有成功。我遇到的主要问题是分隔符是空格,然后我想将双引号内的所有内容视为参数,但是如果其中一个参数包含引号内的引号怎么办。下面是命令和几个例子:
my_command "regex or text" <"regex or text"|NA> <"text or regex"|NA> integer integer
Example1: my_command "Simple case" NA NA 2 3
Example2: my_command "This is it!" "[\",;']" "Really?" 3 5
Example3: my_command "Not so fast" NA "Another regex int the mix [\"a-zA-Z123]" 1 1
基本上 parseCommand(String str) 将采用上述任何示例并返回具有以下值的 List:
Example1: list[0] = "Simple Case", list[1] = NA, list[2] = NA, list[3] = "2", list[4] = "3"
Example2: list[0] = "This is it!", list[1] = "[\",;']", list[2] = NA, list[3] = "3", list[4] = "5"
Example3: list[0] = "Not so fast", list[1] = NA, list[2] = "Another regex int the mix [\"a-zA-Z123]" , list[3] = "1", list[4] = "1"
提前感谢您的帮助。
【问题讨论】: