如何将所有匹配的部分获取到正则表达式模式
How to get all matched parts to regex pattern
我必须分 3 个阶段解析字符串。只有第一阶段有效,在第 2 阶段和第 3 阶段 matcher.groupCount() returns 0 - 这意味着它什么也没找到。我正在在线测试器中测试我的正则表达式,一切正常。但在这里它不起作用。所以问题是也许我错过了什么或者正则表达式有错误?
String rawText = "ashjdajsdg:[requiredPartForFirstPattern]}asdassdasd";
Pattern firstPattern = Pattern.compile("(:\[)(.*?)(\]})");
List<String> firstList = parseContent(rawText, firstPattern);
执行后 firstList 应该只包含一个值(在本例中):"requiredPartForFirstPattern"(可以是任何字符或任何字符序列)。
现在我正在迭代 firstList 中的所有值并使用 2 模式检查它们:
firstList 中的所有值都将采用这种形式:“[someText1]、[someText2]、[someText3]”。
String rawText = "[someText1],[someText2],[someText3]";
Pattern secondPattern = Pattern.compile("(\[([^]]*)\])");
List<String> secondList = parseContent(rawText, secondPattern);
执行后 secondList 应包含以下值(在本例中):"someText1"、"someText2"、"someText3".
终于到了第三阶段。我迭代 secondList 中的所有值并使用 3 模式检查它们。 secondList 中的所有值都将具有以下形式:
“'someValue1','someValue2'”.
String rawText = "'someValue1','someValue2'";
Pattern thirdPattern = Pattern.compile("('(.*?)')");
List<String> thirdList = parseContent(rawText, secondPattern);
执行后 secondList 应包含以下值(在本例中):"someValue1","someValue2".
我的 parseContent 方法:
private List<String> parseContent(String content, Pattern pattern) {
List<String> matchedList = new LinkedList<>();
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
for(int matchIndex = 0; matchIndex < matcher.groupCount(); ++matchIndex) {
matchedList.add(matcher.group(matchIndex));
}
}
return matchedList;
}
You should have while (matcher.find()) instead of an if-statement.
if (matcher.find()) {
for(int matchIndex = 0; matchIndex < matcher.groupCount(); ++matchIndex) {
matchedList.add(matcher.group(matchIndex));
}
}
我已经用下面的代码替换了上面的代码:
while (matcher.find()) {
matchedList.add(matcher.group(1));
}
工作正常,需要帮助。
我必须分 3 个阶段解析字符串。只有第一阶段有效,在第 2 阶段和第 3 阶段 matcher.groupCount() returns 0 - 这意味着它什么也没找到。我正在在线测试器中测试我的正则表达式,一切正常。但在这里它不起作用。所以问题是也许我错过了什么或者正则表达式有错误?
String rawText = "ashjdajsdg:[requiredPartForFirstPattern]}asdassdasd";
Pattern firstPattern = Pattern.compile("(:\[)(.*?)(\]})");
List<String> firstList = parseContent(rawText, firstPattern);
执行后 firstList 应该只包含一个值(在本例中):"requiredPartForFirstPattern"(可以是任何字符或任何字符序列)。
现在我正在迭代 firstList 中的所有值并使用 2 模式检查它们:
firstList 中的所有值都将采用这种形式:“[someText1]、[someText2]、[someText3]”。
String rawText = "[someText1],[someText2],[someText3]";
Pattern secondPattern = Pattern.compile("(\[([^]]*)\])");
List<String> secondList = parseContent(rawText, secondPattern);
执行后 secondList 应包含以下值(在本例中):"someText1"、"someText2"、"someText3".
终于到了第三阶段。我迭代 secondList 中的所有值并使用 3 模式检查它们。 secondList 中的所有值都将具有以下形式: “'someValue1','someValue2'”.
String rawText = "'someValue1','someValue2'";
Pattern thirdPattern = Pattern.compile("('(.*?)')");
List<String> thirdList = parseContent(rawText, secondPattern);
执行后 secondList 应包含以下值(在本例中):"someValue1","someValue2".
我的 parseContent 方法:
private List<String> parseContent(String content, Pattern pattern) {
List<String> matchedList = new LinkedList<>();
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
for(int matchIndex = 0; matchIndex < matcher.groupCount(); ++matchIndex) {
matchedList.add(matcher.group(matchIndex));
}
}
return matchedList;
}
You should have while (matcher.find()) instead of an if-statement.
if (matcher.find()) {
for(int matchIndex = 0; matchIndex < matcher.groupCount(); ++matchIndex) {
matchedList.add(matcher.group(matchIndex));
}
}
我已经用下面的代码替换了上面的代码:
while (matcher.find()) {
matchedList.add(matcher.group(1));
}
工作正常,需要帮助。