For example, I have these strings
APPLEJUCE1A
APPLETREE2B
APPLECAKE3C
APPLETEA1B
APPLEWINE3B
APPLEWINE1C
I want all of these strings except those that have TEA
or WINE1C
in them.
APPLEJUCE1A
APPLETREE2B
APPLECAKE3C
APPLEWINE3B
I've already tried the following, but it didn't work:
^APPLE(?!.*(?:TEA|WINE1C)).*$
Any help is appreciated as I'm also kinda new to this.
CodePudding user response:
You can use
^APPLE(?!.*TEA)(?!.*WINE1C).*
See the regex demo.
Details:
^
- start of stringAPPLE
- a fixed string(?!.*TEA)
- noTEA
allowed anywhere to the right of the current location(?!.*WINE1C)
- noWINE1C
allowed anywhere to the right of the current location.*
- any zero or more chars other than line break chars as many as possible.
CodePudding user response:
If you don't want to match a string that has both or them (which is not in the current example data):
^APPLE(?!.*(WINE1C|TEA).*(?!\1)(?:TEA|WINE1C)).*
Explanation
^
Start of stringAPPLE
match literally(?!
Negative lookahead.*(WINE1C|TEA)
Capture either one of the values in group 1.*
Match 0 characters(?!\1)(?:TEA|WINE1C)
Match either one of the values as long as it is not the same as previously matched in group 1
)
Close the lookahead.*
Match the rest of the line
CodePudding user response:
If you indeed have mutliple strings as you claim, there's no need to jam all that in one regex pattern.
/^APPLE/ && !/TEA|WINE1C/
If you have a single string, the best approach is probably to splice it into lines (split /\n/
), but you could also use a single regex match too
/^APPLE(?!.*TEA|WINE1C).*/g