Java 拆分/标记化/扫描知道引号的字符串
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 根据你的逻辑,你有撇号和单引号的区别,也就是说,有和在我的邻居 如果你想得到上面提到
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