Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 拆分/标记化/扫描知道引号的字符串_Java_String_Quotation Marks - Fatal编程技术网

Java 拆分/标记化/扫描知道引号的字符串

Java 拆分/标记化/扫描知道引号的字符串,java,string,quotation-marks,Java,String,Quotation Marks,Java中是否有一种默认/简单的方法来分割字符串,但要注意引号或其他符号 例如,鉴于本文: There's "a man" that live next door 'in my neighborhood', "and he gets me down..." 获得: There's a man that live next door in my neighborhood and he gets me down 根据你的逻辑,你有撇号和单引号的区别,也就是说,有和在我的邻居 如果你想得到上面提到

Java中是否有一种默认/简单的方法来分割字符串,但要注意引号或其他符号

例如,鉴于本文:

There's "a man" that live next door 'in my neighborhood', "and he gets me down..."
获得:

There's
a man
that
live
next
door
in my neighborhood
and he gets me down

根据你的逻辑,你有撇号和单引号的区别,也就是说,
在我的邻居


如果你想得到上面提到的东西,你必须开发某种配对逻辑。我在想正则表达式。或者某种两部分解析。

类似的东西适用于您的输入:

    String text = "There's \"a man\" that live next door "
        + "'in my neighborhood', \"and he gets me down...\"";

    Scanner sc = new Scanner(text);
    Pattern pattern = Pattern.compile(
        "\"[^\"]*\"" +
        "|'[^']*'" +
        "|[A-Za-z']+"
    );
    String token;
    while ((token = sc.findInLine(pattern)) != null) {
        System.out.println("[" + token + "]");
    }
以上打印内容():

它使用,其中正则表达式模式是:

"[^"]*"      # double quoted token
'[^']*'      # single quoted token
[A-Za-z']+   # everything else
毫无疑问,这并不总是100%有效;引号可以嵌套的情况将很棘手

工具书类

您是否已经想到了特定的解析规则?当前您似乎希望在空格和逗号上拆分,但将单引号和双引号文本保持为单数匹配。还有嵌套的引号需要担心吗?是的。我试着用一个简单的例子,单引号有两个“含义”。我觉得逻辑不是很难理解,这就是我要求的原因。谢谢你的解决方案。添加解释转义双引号和单引号的功能怎么样?例如:
有一个“男人”谁…
"[^"]*"      # double quoted token
'[^']*'      # single quoted token
[A-Za-z']+   # everything else