Java-匹配特定的句型,如果缺少元素,则引发错误

Java-匹配特定的句型,如果缺少元素,则引发错误,java,pattern-matching,Java,Pattern Matching,我希望能够搜索字符串中的特定模式,然后将每个字符串添加到不同的列表中,该列表如下所示: List<String> string1 = new ArrayList<>(); List<String> string2 = new ArrayList<>(); List<Integer> int1 = new ArrayList<>(); List<Integer> int2 = new ArrayList<&g

我希望能够搜索字符串中的特定模式,然后将每个字符串添加到不同的列表中,该列表如下所示:

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
String str = "2 someword 3 word anotherword: , 5" //Note: the 1st integer is missing and is replaced with an empty space
到目前为止,我很好,假设字符串中存在一个子字符串,它通过使用正则表达式
\\d+
[a-zA-Z]+
与模式匹配。但是,如果我得到以下信息:

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
String str = "2 someword 3 word anotherword: , 5" //Note: the 1st integer is missing and is replaced with an empty space
我希望它将丢失的整数注册为一个事实,即它在以下特定模式中丢失:
string:int,int
,然后返回一个错误

编辑:
我想要的模式是:

"string string: int, int"

“\\s+”
更改为
”,\\s+):\\s+\\s+“

如果模式是
字符串字符串:int,int
并且字符串是
2个单词3个单词另一个单词: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

//查找与模式匹配的子字符串
。什么图案?如果不清楚,很抱歉。它:
string:int,int
我想这是你的家庭作业,你想在这里找到答案?我猜。这有点像是我最终想要的一部分的简化版本。在这里,我只想弄清楚如何使用模式在字符串中搜索,因为我找不到关于使用长字符串模式的教程(只有我已经实现的针对单个单词的正则表达式)。没有找到我的任务的答案。正则表达式是否应该有一个组让匹配者工作<代码>(\\w+)(\\w+):(\\d+),(\\d+)为什么需要组?用于while循环?它在正则表达式中没有组的情况下工作
matcher.group()
返回与上一个匹配匹配的输入子序列。编辑答案以包括groupsweacome to stack overflow:-)请查看。您应该提供一些信息,说明代码解决问题的原因。只有代码的答案对社区没有用处。