1

我有一个带有模式的日志。问题是最后它与普通的有点不同。

a>  nc,71802265,0,"Tuesday, June 26, 2012 09:06:49 UTC",38.8335,-122.8072,1.6,0.00,21,"Northern California"
b>  ci,11127314,0,"Tuesday, June 26, 2012 08:37:52 UTC",34.2870,-118.3360,2.2,10.20,100,"Greater Los Angeles area, California"
c>  us,b000aqpn,6,"Tuesday, June 26, 2012 08:29:55 UTC",53.4819,-165.2794,4.4,25.60,96,"Fox Islands, Aleutian Islands, Alaska"

String regex = "^\\"[a-z,A-Z]\\s*\\(,)*[a-z,A-Z]\\"";
Pattern p = Pattern.compile(regex, Pattern.MULTILINE);

从 a 我需要 --- “北加州” 从 b 我需要 --- “大洛杉矶地区,加利福尼亚”等等

谢谢

4

4 回答 4

1

您可以使用String#lastIndexOf, 从倒数第二个字符开始找到第一个"

    String s = "a>  nc,71802265,0,\"Tuesday, June 26, 2012 09:06:49 UTC\",38.8335,-122.8072,1.6,0.00,21,\"Northern California\"";
    int start = s.lastIndexOf("\"", s.length() - 2) + 1;
    String location = s.substring(start, s.length() - 1);
于 2012-07-09T12:03:38.673 回答
0

为什么不使用String.split(regex, limit)并指定您需要拆分的逗号数。

这样你就可以用逗号完整地得到最后一个字段,然后简单地去掉双引号。

于 2012-07-09T12:04:19.897 回答
0

使用$锚来告诉你的匹配应该在行尾:

String lines = "a>  nc,71802265,0,\"Tuesday, June 26, 2012 09:06:49 UTC\",38.8335,-122.8072,1.6,0.00,21,\"Northern California\"\nb>  ci,11127314,0,\"Tuesday, June 26, 2012 08:37:52 UTC\",34.2870,-118.3360,2.2,10.20,100,\"Greater Los Angeles area, California\"\nc>  us,b000aqpn,6,\"Tuesday, June 26, 2012 08:29:55 UTC\",53.4819,-165.2794,4.4,25.60,96,\"Fox Islands, Aleutian Islands, Alaska\"";
    String regex = "\"[^\"]*\"$";
    Matcher m = Pattern.compile(regex, Pattern.MULTILINE).matcher(lines);
    while (m.find()) {
        System.out.println(m.group());
    }

输出:

"Northern California"
"Greater Los Angeles area, California"
"Fox Islands, Aleutian Islands, Alaska"
于 2012-07-09T12:04:52.790 回答
0
for(String s: log.split("\n")){
    System.out.println(s.replaceAll(".+(\".+\")$","$1"));
 }
于 2012-07-09T12:33:18.260 回答