Home > Blockchain >  Regex to exclude a string from anywhere, but match another expression
Regex to exclude a string from anywhere, but match another expression

Time:08-25

I'm quite new to regex. Tried to look at other questions but still can't workout how to resolve my scenario. I want to match string that starts with "AB" but not ABC,or string contains DE but not DEF. For example sDEN23, DET or DE should be matches, AB3 should be matches. I've tried the below so far but it doesn't work as expected. Could someone please help? Many thanks.

Edited: How can this be achieved without using lookahead and lookbehind, as these are not support by Impala?

 .*AB?[^C].*|.*DE?[^DEF]

CodePudding user response:

You can use a negative lookahead pattern to avoid matches followed by certain characters:

^(?:AB(?!C)|(?!.*DEF).*DE).*

Demo: https://regex101.com/r/DH1WTf/3

EDIT: Since you've updated the question by replacing the python tag with impala, whose regex engine does not support lookarounds, you can instead use multiple LIKE operators to achieve what you want:

SELECT * FROM table_name WHERE (col LIKE 'AB%' OR col LIKE '           
  • Related