标记字符串但忽略引号内的分隔符 [英] Tokenizing a String but ignoring delimiters within quotes

查看:29
本文介绍了标记字符串但忽略引号内的分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有以下字符串

!cmd 45 90 "An argument" Another AndAnother "Another one in quotes"

变成下面的数组

{ "!cmd", "45", "90", "An argument", "Another", "AndAnother", "Another one in quotes" }

我试过了

new StringTokenizer(cmd, """)

但这会将Another"和AndAnother"返回为Another AndAnother",这不是预期的效果.

but this would return "Another" and "AndAnother as "Another AndAnother" which is not the desired effect.

谢谢.

我又改了这个例子,这次我相信它最好地解释了情况,虽然它和第二个例子没有什么不同.

I have changed the example yet again, this time I believe it explains the situation best although it is no different than the second example.

推荐答案

使用 java.util.regex.Matcher 并执行 find() 而不是任何类型的 split 在这些场景中.

It's much easier to use a java.util.regex.Matcher and do a find() rather than any kind of split in these kinds of scenario.

也就是说,不是为标记之间的分隔符定义模式,而是为标记本身定义模式.

That is, instead of defining the pattern for the delimiter between the tokens, you define the pattern for the tokens themselves.

这是一个例子:

    String text = "1 2 "333 4" 55 6    "77" 8 999";
    // 1 2 "333 4" 55 6    "77" 8 999

    String regex = ""([^"]*)"|(\S+)";

    Matcher m = Pattern.compile(regex).matcher(text);
    while (m.find()) {
        if (m.group(1) != null) {
            System.out.println("Quoted [" + m.group(1) + "]");
        } else {
            System.out.println("Plain [" + m.group(2) + "]");
        }
    }

以上打印(在 ideone.com 上看到):

Plain [1]
Plain [2]
Quoted [333 4]
Plain [55]
Plain [6]
Quoted [77]
Plain [8]
Plain [999]

模式本质上是:

"([^"]*)"|(S+)
 \_____/  \___/
    1       2

有 2 个备选:

  • 第一个替代匹配开头的双引号,除双引号以外的任何序列(在第 1 组中捕获),然后是结尾的双引号
  • 第二个备选匹配第 2 组中捕获的任何非空白字符序列
  • 交替的顺序在这个模式中很重要

请注意,这不会处理引用段内的转义双引号.如果您需要这样做,那么模式会变得更加复杂,但 Matcher 解决方案仍然有效.

Note that this does not handle escaped double quotes within quoted segments. If you need to do this, then the pattern becomes more complicated, but the Matcher solution still works.

  • regular-expressions.info/Brackets for Grouping and Capturing, Alternation with Vertical Bar, Character Class, Repetition with Star and Plus
  • regular-expressions.info/Examples - Programmer - Strings - for pattern with escaped quotes

注意StringTokenizer 是一个遗留类.推荐使用java.util.ScannerString.split,当然还有 java.util.regex.Matcher 以获得最大的灵活性.

Note that StringTokenizer is a legacy class. It's recommended to use java.util.Scanner or String.split, or of course java.util.regex.Matcher for most flexibility.

  • Difference between a Deprecated and Legacy API?
  • Scanner vs. StringTokenizer vs. String.Split
  • Validating input using java.util.Scanner - has many examples

这篇关于标记字符串但忽略引号内的分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆