我有一个遵循这种格式的字符串:
"<N>|<M>|<word_of_size_N><word_of_size_M>"
是否有一个single正则表达式可以找到
<N>
和<M>
的值并使用这些值来提取<word_of_size_N>
和<word_of_size_M>
?
我试过以下片段
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
String input = "5|3|hello123";
String regex = "^(\\d+)\\|(\\d+)\\|(\\w{\\1})(\\w{\\2})$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
String n = matcher.group(1);
String m = matcher.group(2);
String wordN = matcher.group(3);
String wordM = matcher.group(4);
System.out.println("The input string matches the regex pattern.");
System.out.println("N = " + n + ", M = " + m);
System.out.println("word of size N = " + wordN);
System.out.println("word of size M = " + wordM);
} else {
System.out.println("The input string does not match the regex pattern.");
}
}
}
但它引发了一个例外:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 17
^(\d+)\|(\d+)\|(\w{\1})(\w{\2})$
^
at java.base/java.util.regex.Pattern.error(Pattern.java:2028)
at java.base/java.util.regex.Pattern.closure(Pattern.java:3323)
at java.base/java.util.regex.Pattern.sequence(Pattern.java:2214)
at java.base/java.util.regex.Pattern.expr(Pattern.java:2069)
at java.base/java.util.regex.Pattern.group0(Pattern.java:3060)
at java.base/java.util.regex.Pattern.sequence(Pattern.java:2124)
at java.base/java.util.regex.Pattern.expr(Pattern.java:2069)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1783)
at java.base/java.util.regex.Pattern.<init>(Pattern.java:1429)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1069)
at RegexExample.main(RegexExample.java:7)
我知道我可以简单地分两步完成
<N>
和 <M>
<N>
和<M>
但我很想知道正则表达式是否可以以某种方式引用自身。
奖励: 还有其他正则表达式引擎可以做到这一点吗?