简体   繁体   English

正则表达式字之间

[英]Regex Between word

I have strings 我有琴弦

Yangebup, Perth, Australia, Dated 2011-01-09 Yanebup,珀斯,澳大利亚,日期为2011-01-09

Jakarta, Indonesia, Dated 2013-08-24 印尼雅加达,日期:2013-08-24

So I want to get the location name Yangebup, Perth, Australia and Jakarta, Indonesia 因此,我想获取位置名称: Yangebup, Perth, AustraliaJakarta, Indonesia

How can I get that location name with regex? 如何使用正则表达式获取该位置名称? I have tried to use this code .+?,\\bDated\\b but the result is Match not found. 我尝试使用此代码.+?,\\bDated\\b但结果是未找到匹配项。

I also tried with regexpal.com to test my regex, with this code .+\\bdated\\b I can get it, but i have to use the Case insensitive (i) and the result still nothing when I write it on my java program. 我也用regexpal.com尝试使用此代码测试我的正则表达式.+\\bdated\\b

Use this regex pattern: 使用此正则表达式模式:

^(.*), Dated \d{4}-\d{2}-\d{2}$

List<String> locations = new ArrayList<String>();
locations.add("Yangebup, Perth, Australia, Dated 2011-01-09");
locations.add("Jakarta, Indonesia, Dated 2011-01-09");

String pattern = "^(.*), Dated \\d{4}-\\d{2}-\\d{2}$";
Pattern r = Pattern.compile(pattern);

for (String location : locations) {
    Matcher m = r.matcher(location);
    if (m.find()) {
        System.out.println("Found a location: " + m.group(1) );
    } else {
        System.out.println("NO MATCH");
    }
}

Output: 输出:

Found a location: Yangebup, Perth, Australia
Found a location: Jakarta, Indonesia

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

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