简体   繁体   中英

multiple groups with the same name regex java?

是否有任何 Java 正则表达式 api 或库可以在一种模式中接受多个具有相同名称的组?

This is a very old question, but google brought me here, I found a solution for PHP using the /J modifier. All details explained here: http://www.rexegg.com/regex-capture.html#dupe_names

In .NET, PCRE (C, PHP, R…), Perl and Ruby, you can use the same group name at various places in your pattern. (In PCRE you need to use the (?J) modifier or PCRE_DUPNAMES option.) In these engines, this regex would be valid:

:(?<token>\d+)|(?<token>\d+)#

Working example here https://regex101.com/r/h7HJXj/1

alternative of (ab(?<a>[\\w]*)|bs(?<a>[\\w]*)) regex is (ab|bs)(?<a>[\\w]*) with same name. check test in the demo link with (ab(?<a>[\\w]*)|bs(?<b>[\\w]*)) and (ab|bs)(?<a>[\\w]*) . these gives you same result.

demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM