【问题标题】:Command parsing with quotes within quotes用引号内的引号解析命令
【发布时间】: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"

提前感谢您的帮助。

【问题讨论】:

    标签: java regex


    【解决方案1】:

    尝试使用正则表达式是错误的 - 您没有解析 regular expression

    从这样的开始 - 您将使用正则表达式 fail

    public void test() {
        System.out.println(parse("\"This is it!\" \"[\\\",;']\" \"Really?\" 3 5"));
    }
    
    List<String> parse(String s) {
        List<String> parsed = new ArrayList<String>();
        boolean inQuotes = false;
        boolean escape = false;
        int from = 0;
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            switch (ch) {
                case ' ':
                    if (!inQuotes && !escape) {
                        parsed.add(s.substring(from, i));
                        from = i + 1;
                    }
                    break;
                case '\"':
                    if (!escape) {
                        inQuotes = !inQuotes;
                    }
                    escape = false;
                    break;
                case '\\':
                    escape = !escape;
                    break;
                default:
                    escape = false;
                    break;
            }
        }
    
        if (from < s.length()) {
            parsed.add(s.substring(from, s.length()));
        }
        return parsed;
    }
    

    已添加

    对于有问题的特定字符串,这是我的解释:

    String str = "my_command \"Something [\"abc']\" \"text\" NA 1 1";
    //                         ............        ..       .......
    //                        ^            ^      ^  ^     ^
    

    我使用^ 表示引号,并使用. 表示所有字符,因此在引号中。因此,在第一个引号之后没有进一步的拆分,因为在那之后没有未引用的空格。

    【讨论】:

    • 感谢您的快速回复,但这并不完全奏效。尝试了以下内容: String str = "my_command \"Something [\"abc']\" \"text\" NA 1 1".
    • @user2182414 - 我现在得到 [my_command, "Something ["abc']" "text" NA 1 1] 哪种有意义,因为所有空格都在第一个空格之后的引号中 - 做了什么你期待吗?PS:我稍微调整了\" 的情况。
    • 我期待:list[0]=my_command, list[1]=Something ["abc], list[2]=text, list[3]=1, list[4]=1 . 使用上述方法,我得到 list[0] = my_command list[1]= "Something ["abc']" "text" NA 1 1
    • 但是 abc] 后面的空格是用引号引起来的,所以不应该中断。
    • 引号内的空格不应中断。因此,例如“Hello number 2”将被视为一个参数/项目,“Hello number \"2\"” 也将是一个参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2017-11-25
    • 2019-10-14
    • 2018-09-05
    • 1970-01-01
    相关资源
    最近更新 更多