regexnsregularexpression

how to simplify this regular expression


how can I simplify this expression without so many ORs?

a(r|i|ir|ri|irr|rri|rr)(ba|b|ab)

The goal is to catch all of these variations:

ariba, airba, arba, arib, airb, arriba, arrba, airrba, airab, ariab

You can see it here: https://regex101.com/r/vbQRGF/1


Solution

  • If your goal is for it to shorter, then that is possible, but it will match more than what you've specified:

    a[ri]{1,3}[ab]{1,2}