当在Java中匹配组时,是否有可能解析标识的模式组名称?
例如为:
组合模式:(?<A_Pattern>A\n)|(?<B_Pattern>B\n)|(?<C_Pattern>C\n)
计划:
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(fromFile(logFilename));
// Find all matches
while (matcher.find()) {
// should output e.g. A_Pattern
System.out.println("Found pattern: " + matcher.groupName());
}
背景: 我正在开发一个程序来在日志文件中搜索正则表达式。这不是问题,但我有三大问题:
因此,我将模式(正则表达式)连接到一个表达式中,并尝试将名称添加为组名,但我没有在Java中找到用于解析标识名称的选项。
是否有可能使用正则表达式组名称或任何其他解决方案执行此操作。组名只是一个尝试。
答案 0 :(得分:2)
如果你知道你的子表达式不包含捕获组,那么你可以这样做:
String regex = "(A\n)|(B\n)|(C\n)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
// Find all matches
while (matcher.find()) {
if (matcher.group(1) != null)
System.out.println("Found pattern A at index " + matcher.start(1));
else if (matcher.group(2) != null)
System.out.println("Found pattern B at index " + matcher.start(2));
else
System.out.println("Found pattern C at index " + matcher.start(3));
}
如果您要匹配N个模式,则允许模式包含捕获组,并且您需要知道匹配的模式,您可以像这样分配命名组:
String[] patterns = { "ABC", "D(EF)", "XYZ" };
String input = "Lorem ipsum DEF dolor sit ABC amet, XYZ consectetur DEF adipiscing elit";
String[] names = new String[patterns.length];
StringBuilder regex = new StringBuilder();
for (int i = 0; i < patterns.length; i++) {
Pattern.compile(patterns[i]); // validate
names[i] = "Pattern" + (i+1);
if (i != 0)
regex.append('|');
regex.append("(?<").append(names[i]).append('>').append(patterns[i]).append(')');
}
Pattern pattern = Pattern.compile(regex.toString());
for (Matcher matcher = pattern.matcher(input); matcher.find(); )
for (int i = 0; i < patterns.length; i++)
if (matcher.start(names[i]) != -1) {
System.out.println("Found " + names[i] +
" at index " + matcher.start(names[i]) +
": " + matcher.group(names[i]));
break;
}
输出:
Found Pattern2 at index 12: DEF
Found Pattern1 at index 26: ABC
Found Pattern3 at index 36: XYZ
Found Pattern2 at index 52: DEF
正如您所看到的,匹配检测工作正常,即使是第二种模式中的捕获组也是如此。