my regex i wrote is (a|b)-(?(1)(1)|(2))
what it is supposed to do:
if a
then a-1
allowed
if b
then b-2
allowed
having group 1 = a or b and group 2 = 1 or 2
i have tried using ((?'a'a)|b)-(?(a)(1)|(2))
but it gave me different groups for numbers being group 3 or 4
how can i keep numbers group to 2
答案 0 :(得分:1)
使用zero-width positive lookahead非捕获组。
(a(?=-1)|b(?=-2))-(1|2)
仅在a
后跟a
时匹配-1
。
仅在b
后跟b
时匹配-2
。
然后匹配-
和1
或2
。
捕获组1是a
或b
。
捕获组2为1
或2
。