Java - 匹配特定句子模式,如果缺少元素则会引发错误

时间:2017-05-03 04:47:36

标签: java pattern-matching

我希望能够在字符串中搜索特定模式,然后将每个字符串添加到不同的列表中,该列表遵循以下内容:

List<String> string1 = new ArrayList<>();
List<String> string2 = new ArrayList<>();
List<Integer> int1 = new ArrayList<>();
List<Integer> int2 = new ArrayList<>();
//Note: Pattern I want = string string: int, int
String str = "2 someword 3 word anotherword: 7, 5"; //find the substring which matches the pattern (note there is a ':' after the second word and a ',' after the first integer)
String[] splited = str.split("\\s+");
for (int i = 0; i < splitted.size(); i++) {
    //if the front 3 (i+1, i+2, i+3) are word, int, int then string1.add(splitted.get(i))
    //do similar for the 2nd: check(i-1, i+1, i+2) and add to str2
    //do similar for the 3rd: check(i-2, i-1, i+2) add to int1
    //do similar for the 4th: check(i-3, i-2, i-1) add to int2
}

//then System.out.println for all all 4 lists

预期结果

[word] //splitted.get(3)
[anotherword] //splitted.get(4)
[7] //splitted.get(5)
[5] //splitted.get(6)

到目前为止,我很好,假设在字符串中,存在一个与模式匹配的子字符串,使用正则表达式\\d+[a-zA-Z]+。但是,如果我得到以下内容:

String str = "2 someword 3 word anotherword: , 5" //Note: the 1st integer is missing and is replaced with an empty space

我希望它将缺少的整数注册为以下特定模式中缺少的事实:string string: int, int然后返回错误。

修改
我想要的模式是:

"string string: int, int"

2 个答案:

答案 0 :(得分:0)

"\\s+"更改为",\\s+|:\\s+|\\s+"

答案 1 :(得分:0)

如果模式为string string: int, int且字符串为2 someword 3 word anotherword: 7, 5,则此程序可满足您的需求:

public static void main(String[] args) {
    String str = "2 someword 3 word anotherword: 7, 5";
    Pattern pattern = Pattern.compile("(\\w+) (\\w+:) (\\d+), (\\d+)");

    Matcher matcher = pattern.matcher(str);

    while(matcher.find()) {
        String word = matcher.group(1);
        String anotherword = matcher.group(2);
        String str7 = matcher.group(3);
        String str5 = matcher.group(4);
        System.out.println(word+" "+anotherword+" "+str7+" "+str5);
    }
}

打印:

word anotherword: 7, 5