regex to select 2nd option from group value (a|b)

时间:2019-02-24 03:14:01

标签: regex if-statement

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

1 个答案:

答案 0 :(得分:1)

使用zero-width positive lookahead非捕获组。

(a(?=-1)|b(?=-2))-(1|2)

仅在a后跟a时匹配-1
仅在b后跟b时匹配-2
然后匹配-12
捕获组1是ab
捕获组2为12