无法将多个正则表达式模式添加到数组列表中

时间:2018-08-01 09:28:08

标签: java arrays regex arraylist regex-group

我的目标是找到一个模式时给它一个字符串,然后将它们添加到单独的数组中。

例如

我的测试字符串是

String input = "this is a sentence continents=Asia end continents=Europe end continents=Africa end continents=Australia end continents=South America end continents=North America end continents=Antartica end";

结果是

[continents, Asia, continents, Europe, continents, Africa, continents, Australia, continents, South America, continents, North America]

我想要的是将它们添加到单独的数组中,如下所示

[continents, Asia], [continents, Europe], [continents, Africa], [continents, Australia], [continents, South America], [continents, North America], [continents, Antartica]

下面是我的代码:

list = new ArrayList<>();

Pattern pattern = Pattern.compile("continents=(.+?) end ");
Matcher matcher = pattern.matcher(input);

while (matcher.find()) { 
    list.add("continents");
    value = matcher.group(1);
    list.add(value);
} 

任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,那么您就快到了。您只需将每个匹配项添加到不同的列表,然后将该列表添加到外部列表:

    List<List<String>> list = new ArrayList<>();

    Pattern pattern = Pattern.compile("continents=(.+?) end ");
    Matcher matcher = pattern.matcher(input);

    while (matcher.find()) { 
        List<String> innerList = new ArrayList<>();
        innerList.add("continents");
        String value = matcher.group(1);
        innerList.add(value);
        list.add(innerList);
    }

答案 1 :(得分:1)

您可以使用

String input = "this is a sentence continents=Asia end continents=Europe end continents=Africa end continents=Australia end continents=South America end continents=North America end continents=Antartica end";
Pattern pattern = Pattern.compile("(continents)=(.+?) end ");
Matcher matcher = pattern.matcher(input);
List<List<String>> lst = new ArrayList<>();
while (matcher.find()){
    lst.add(Arrays.asList(matcher.group(1),matcher.group(2))); 
} 
System.out.println(lst); 
// => [[continents, Asia], [continents, Europe], [continents, Africa], [continents, Australia], [continents, South America], [continents, North America]]

请参见Java demo

正则表达式现在有两个捕获组,continents被捕获到组1中,名称被捕获到了组2中。匹配结果与Arrays.asList(matcher.group(1),matcher.group(2))一起添加到List<List<String>>

请注意,您还可以使用诸如"(continents)=(.+?)\\s+end\\b"之类的正则表达式来支持所有空格,并且end作为一个完整的单词,甚至可以在字符串末尾或任何非单词char之前匹配。 / p>