繁体   English   中英

使用Regex从Java中的字符串中提取序列

[英]Extract a sequence from a string in java using Regex

我有一个带有模式的日志。 最后一点与常规有所不同。

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我需要---“加利福尼亚大洛杉矶地区”,依此类推

谢谢

您可以使用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);

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

这样,您可以使用逗号将最后一个字段保持不变 ,然后只需去除双引号即可。

使用$锚指示您的匹配项应位于行的末尾:

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"
for(String s: log.split("\n")){
    System.out.println(s.replaceAll(".+(\".+\")$","$1"));
 }

暂无
暂无

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

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