Regular expression packages have limitations
java.regex package I have confirmed that there is no support for named groups, so please let me know which third-party libraries support it.
I looked into jregex, but the last release was in 2002 and it didn't work for java5 (though I tried it right away).
Support since Java 7
Java 7 added support for named groups. The building blocks to support named capture groups are:
Alternatives for Java 7 and earlier
For Java 7 and earlier, there were the following alternatives:
Regex2 library
Regex2 library supports named groups It extends the java.util.regex.Pattern class to create .
Example
Input string: "TEST 123"
Regular expression: "(? Access: matcher.group(1) == "TEST" Replace: matcher.replaceAll("aaaaa_$1_sssss_$2____") == "aaaaa_TEST_sssss_123____" The above is the detailed content of How can I use named capture groups in Java before Java 7?. For more information, please follow other related articles on the PHP Chinese website!
matcher.group("login") == "TEST"
matcher.name( 1) == "login"
matcher.replaceAll("aaaaa_${login}_sssss_${id}____") == "aaaaa_TEST_sssss_123____"