Reputation: 35
I am trying to come up with a RegEx pattern that takes strings that look like this:
KEEP_THIS_L_1234
KEEP_THIS_R_12
KEEP_THIS
and returns a capture group with this result:
KEEP_THIS
KEEP_THIS
KEEP_THIS
So far I have tried /^(\w+)(?=_(L|R))(?=_\d{0,4})/
, but this pattern only returns the capture groups for the first two instances:
KEEP_THIS
KEEP_THIS
Can someone help me understand what I am missing?
Thanks!
Upvotes: 0
Views: 50
Reputation: 22466
you need to make the last two groups optional, like this:
/^(\w+?)((_(L|R))(_\d{0,4}))?$/
Your desired result will always be in $1.
This has the advantage that your other data captured (if any) will be in groups $2 and $3.
Upvotes: 1