What is the equivalent of branch reset operator (“?|”) found in php(pcre) in C#?

Deadly 提交于 2019-11-28 11:13:05

.NET doesn't support the branch-reset operator, but it does support named groups, and it lets you reuse group names without restriction (something no other flavor does, AFAIK). So you could use this:

(?:(?<abbr>Sat)ur|(?<abbr>Sun))day

...and the abbreviated name will be stored in Match.Groups["abbr"].

should be possible to concat backref1 and backref2.
As one of each is always empty and a string concat with empty is still the same string...

with your regex (?:(Sat)ur|(Sun))day and replacement $1$2
you get Sat for Saturday and Sun for Sunday.

 regex (?:(Sat)ur|(Sun))day
 input    | backref1 _$1_ | backref2 _$2_ | 'concat' _$1$2_
 ---------|---------------|---------------|----------------
 Saturday | 'Sat'         | ''            | 'Sat'+'' = Sat
 Sunday   | ''            | 'Sun'         | ''+'Sun' = Sun

instead of reading backref1 or backref2 just read both results and concat the result.

You can use the branch-reset operator:

(?|foo(bar)|still(life)|(like)so)

That will only set group one no matter which branch matches.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!