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 + "]");
}
上面的打印(如在 ideone.com 上看到的):
[There's]
["a man"]
[that]
[live]
[next]
[door]
['in my neighborhood']
["and he gets me down..."]
它使用Scanner.findInLine
,其中正则表达式模式是以下之一:
"[^"]*" # double quoted token
'[^']*' # single quoted token
[A-Za-z']+ # everything else
毫无疑问,这并不总是 100% 有效。可以嵌套引号等的情况将很棘手。
根据您的逻辑值得怀疑,您可以区分撇号和单引号,There's
即in my neighborhood
如果您想要上面的内容,则必须开发某种配对逻辑。我在想正则表达式。或某种两部分解析。