java 正则表达式匹配为空
java regex match is null
我试图找到一个特定的模式,但也排除了某些模式。出于某种原因,我的正则表达式在我的程序中不起作用,但它可以与在线正则表达式测试器一起使用。有什么问题?
这里是在线测试:regex101
这是 java 测试:
private void TestRegex() {
ArrayList<String> strings = new ArrayList<>();
strings.add("Every Witch Way 3x19 New Witch Order (2015)");
strings.add("The Tonight Show Starring Jimmy Fallon Episode dated 22 January 2015 (2015)");
strings.add("October Gale (2014)");
strings.add("Kung Pow: Enter the Fist (2002)");
Pattern pattern = Pattern.compile("^((?!.*(\d*x\d*|Episode dated)).*) \((\d*)\)$");
for (String s : strings) {
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
Log.d("TAG1", s);
for (int j=0; j<matcher.groupCount(); j++) {
Log.d("TAG2", "Match " + j + ": " + matcher.group(j));
}
}
}
}
这是我测试的输出:
... D/TAG1﹕ October Gale (2014)
... D/TAG2﹕ Match 0: October Gale (2014)
... D/TAG2﹕ Match 1: October Gale
... D/TAG2﹕ Match 2: null
... D/TAG1﹕ Kung Pow: Enter the Fist (2002)
... D/TAG2﹕ Match 0: Kung Pow: Enter the Fist (2002)
... D/TAG2﹕ Match 1: Kung Pow: Enter the Fist
... D/TAG2﹕ Match 2: null
为什么匹配 2 为空?在在线匹配器中,两者都正确匹配。
正则表达式字符串的解释:
我想匹配格式为 Movie Title (Year)
的所有字符串并忽略所有包含字符串 \d*x\d*
的字符串(示例:1x01
、2x05
、3x11
) 或包含字符串 Episode dated
的字符串,因为这些字符串指的是电视节目剧集,而不是电影,我试图将它们分开。我还需要匹配电影标题和年份。
^((?!.*(?:\d*x\d*|Episode dated)).*) \((\d*)\)$
^^ ^^ ^^
Group1 Group2 Group3
Group2
是你所在的空组 getting.In regex101.com 你的年份 2002
与 group 3
匹配。使第二组不捕获。
当您的字符串由于否定前瞻而匹配时 Group2
不能 there.So 它将为空。
查看演示。
问题主要出在这个j<matcher.groupCount()
条件上。您有三个组,但此条件将仅打印除组 0 之外的两个组。将 <
转为 <=
也将帮助您打印最后一组。
for (int j=0; j<=matcher.groupCount(); j++) {
Log.d("TAG2", "Match " + j + ": " + matcher.group(j));
Why is match 2 null?
这是因为捕获组存在于否定先行断言中。就像其他回答者说的那样,将捕获组转换为非捕获组不会创建额外的组。
Group 0 = Prints the entire match
Group 1 = Prints the characters which are present inside the group index 1.
Group 2 = Prints the characters which are present inside group index 2. Likewise it goes on.
我试图找到一个特定的模式,但也排除了某些模式。出于某种原因,我的正则表达式在我的程序中不起作用,但它可以与在线正则表达式测试器一起使用。有什么问题?
这里是在线测试:regex101
这是 java 测试:
private void TestRegex() {
ArrayList<String> strings = new ArrayList<>();
strings.add("Every Witch Way 3x19 New Witch Order (2015)");
strings.add("The Tonight Show Starring Jimmy Fallon Episode dated 22 January 2015 (2015)");
strings.add("October Gale (2014)");
strings.add("Kung Pow: Enter the Fist (2002)");
Pattern pattern = Pattern.compile("^((?!.*(\d*x\d*|Episode dated)).*) \((\d*)\)$");
for (String s : strings) {
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
Log.d("TAG1", s);
for (int j=0; j<matcher.groupCount(); j++) {
Log.d("TAG2", "Match " + j + ": " + matcher.group(j));
}
}
}
}
这是我测试的输出:
... D/TAG1﹕ October Gale (2014)
... D/TAG2﹕ Match 0: October Gale (2014)
... D/TAG2﹕ Match 1: October Gale
... D/TAG2﹕ Match 2: null
... D/TAG1﹕ Kung Pow: Enter the Fist (2002)
... D/TAG2﹕ Match 0: Kung Pow: Enter the Fist (2002)
... D/TAG2﹕ Match 1: Kung Pow: Enter the Fist
... D/TAG2﹕ Match 2: null
为什么匹配 2 为空?在在线匹配器中,两者都正确匹配。
正则表达式字符串的解释:
我想匹配格式为 Movie Title (Year)
的所有字符串并忽略所有包含字符串 \d*x\d*
的字符串(示例:1x01
、2x05
、3x11
) 或包含字符串 Episode dated
的字符串,因为这些字符串指的是电视节目剧集,而不是电影,我试图将它们分开。我还需要匹配电影标题和年份。
^((?!.*(?:\d*x\d*|Episode dated)).*) \((\d*)\)$
^^ ^^ ^^
Group1 Group2 Group3
Group2
是你所在的空组 getting.In regex101.com 你的年份 2002
与 group 3
匹配。使第二组不捕获。
当您的字符串由于否定前瞻而匹配时 Group2
不能 there.So 它将为空。
查看演示。
问题主要出在这个j<matcher.groupCount()
条件上。您有三个组,但此条件将仅打印除组 0 之外的两个组。将 <
转为 <=
也将帮助您打印最后一组。
for (int j=0; j<=matcher.groupCount(); j++) {
Log.d("TAG2", "Match " + j + ": " + matcher.group(j));
Why is match 2 null?
这是因为捕获组存在于否定先行断言中。就像其他回答者说的那样,将捕获组转换为非捕获组不会创建额外的组。
Group 0 = Prints the entire match
Group 1 = Prints the characters which are present inside the group index 1.
Group 2 = Prints the characters which are present inside group index 2. Likewise it goes on.