我正尝试在下面的单词列表上使用 stringtokenizer
String sentence=""Name":"jon" "location":"3333 abc street" "country":"usa"" etc
当我使用 stringtokenizer 并将空格作为分隔符时,如下所示
StringTokenizer tokens=new StringTokenizer(sentence," ")
我期望我的输出是如下所示的不同标记
Name:jon
location:3333 abc street
country:usa
但是字符串分词器也尝试对位置的值进行分词,它看起来像
Name:jon
location:3333
abc
street
country:usa
请告诉我如何解决上述问题,如果我需要做一个正则表达式,我应该指定什么样的表达式?
最佳答案
这可以使用 CSV Reader 轻松处理.
String str = "\"Name\":\"jon\" \"location\":\"3333 abc street\" \"country\":\"usa\"";
// prepare String for CSV parsing
CsvReader reader = CsvReader.parse(str.replaceAll("\" *: *\"", ":"));
reader.setDelimiter(' '); // use space a delimiter
reader.readRecord(); // read CSV record
for (int i=0; i<reader.getColumnCount(); i++) // loop thru columns
System.out.printf("Scol[%d]: [%s]%n", i, reader.get(i));
更新:这里是纯 Java SDK 解决方案:
Pattern p = Pattern.compile("(.+?)(\\s+(?=(?:(?:[^\"]*\"){2})*[^\"]*$)|$)");
Matcher m = p.matcher(str);
for (int i=0; m.find(); i++)
System.out.printf("Scol[%d]: [%s]%n", i, m.group(1).replace("\"", ""));
输出:
Scol[0]: [Name:jon]
Scol[1]: [location:3333 abc street]
Scol[2]: [country:usa]
现场演示:http://ideone.com/WO0NK6
说明:根据 OP 的评论:
我正在使用这个正则表达式:
(.+?)(\\s+(?=(?:(?:[^\"]*\"){2})*[^\"]*$)|$)
现在将其分解成更小的 block 。
PS:DQ代表双引号
(?:[^\"]*\") 0 or more non-DQ characters followed by one DQ (RE1)
(?:[^\"]*\"){2} Exactly a pair of above RE1
(?:(?:[^\"]*\"){2})* 0 or more occurrences of pair of RE1
(?:(?:[^\"]*\"){2})*[^\"]*$ 0 or more occurrences of pair of RE1 followed by 0 or more non-DQ characters followed by end of string (RE2)
(?=(?:(?:[^\"]*\"){2})*[^\"]*$) Positive lookahead of above RE2
.+? Match 1 or more characters (? is for non-greedy matching)
\\s+ Should be followed by one or more spaces
(\\s+(?=RE2)|$) Should be followed by space or end of string
简而言之:表示匹配 1 个或多个长度后跟“空格或字符串结尾”的任何字符。空格后必须跟偶数个 DQ。因此,双引号外的空格将被匹配,双引号内的空格将不被匹配(因为它们后面跟着奇数个 DQ)。
关于java - StringTokenizer - 如何忽略字符串中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16110808/