IAmKersplatt
IAmKersplatt

Reputation: 1

Match up to but not including string with multiple underscores

I have a strings for example

Apple_Banana_Orange_PACK101_10
Pear_Apple__Grapes_BUNDLE222_06

I need a regex string that will match everything before _PACK or _BUNDLE and nothing else.

So for example I want the first string to return

Apple_Banana_Orange

I have tried:

If you're looking to capture everything up to "abc":

/^(.*?)(_PACK|_BUN)/

And a few other options but still struggling. Any help appreciated

Upvotes: 0

Views: 45

Answers (2)

Andreas DM
Andreas DM

Reputation: 10998

You can simply do

^(.*)(_PACK|_BUNDLE) 

where group 1 is what you want to catch

Upvotes: 0

SanV
SanV

Reputation: 945

Use Positive Lookahead as follows:

^.*?(?=_PACK|_BUN)

Demo

Upvotes: 1

Related Questions