简体   繁体   English

使用Java String split()作为简单的“解析器”

[英]Using Java String split() as simple “parser”

[{Action=GoTo, Title=0001000a, Page=1 XYZ 7 797 null}, {Action=GoTo, Title=0001000b, Page=3 XYZ 7 797 null}, {Action=GoTo, Title=0001000c, Page=5 XYZ 7 797 null}, {Action=GoTo, Title=0001000d, Page=7 XYZ 7 797 null}] [{Action = GoTo,Title = 0001000a,Page = 1 XYZ 7797 null},{Action = GoTo,Title = 0001000b,Page = 3 XYZ 7797 null},{Action = GoTo,Title = 0001000c,Page = 5 XYZ 7797 null},{Action = GoTo,Title = 0001000d,Page = 7 XYZ 7797 null}]

I'm trying to find the simplest way to parse the above String, all I need are "Title" and "Page". 我试图找到最简单的方法来解析上述字符串,我需要的只是“标题”和“页面”。 So I want a simple String[] = {"0001000a","1","0001000b","3"...} 所以我想要一个简单的String [] = {“ 0001000a”,“ 1”,“ 0001000b”,“ 3” ...}

str.split("(\\[|, )\\{Action=GoTo, Title=|, Page=| XYZ \\d+ \\d+ null\\}");

I have tested the regexp in a few online js regexp tester, it seems fine, but the resulting String[] = {"0001000a","1","","0001000b","3",""...}, an extra empty string after each page value. 我已经在一些在线js regexp测试器中测试了regexp,看起来不错,但是生成的String [] = {“ 0001000a”,“ 1”,“”,“ 0001000b”,“ 3”,“” ...} ,每个页面值后面的一个额外的空字符串。

str.split("\\[|\\{Action=GoTo, Title=|, Page=| XYZ \\d+ \\d+ null\\}(, |\\])");

This one produces String[] = {"","0001000a","1","","0001000b","3"...}, an empty string in front of every title value. 这会产生String [] = {“”,“ 0001000a”,“ 1”,“”,“ 0001000b”,“ 3” ...},这是每个标题值前面的空字符串。

It seems like java doesn't like ", " as regexp, or it could be the way that Java String.split() works!? 似乎Java不喜欢将“,”作为regexp,或者它可能是Java String.split()起作用的方式!

This is easily implemented using plain Regexp instead of split() 使用纯正则表达式而不是split()可以轻松实现

String line = "[{Action=GoTo, Title=0001000a, Page=1 XYZ 7 797 null}, {Action=GoTo, Title=0001000b, Page=3 XYZ 7 797 null}, {Action=GoTo, Title=0001000c, Page=5 XYZ 7 797 null}, {Action=GoTo, Title=0001000d, Page=7 XYZ 7 797 null}]";
ArrayList<String> list = new ArrayList<>();
Pattern pattern = Pattern.compile("Title=([^,]+), Page=([^}]+)}");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
    list.add(matcher.group(1));
    list.add(matcher.group(2));
}
String[] foo = list.toArray(new String[list.size()]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM