2

I was reading this SO post on excluding cases with a given string.

The selected answer there uses ^((?!hede).)*$ to exclude string 'hede'.

I have strings like the following:

apples_IOS_QA
apples_Android_QA
oranges
bananas
banannas_QA
apples_Android
apples_IOS
QA_apples_IOS // note sometimes 'QA' is at the beginning of the string

I'd like to return non QA versions of apples.

Tried (Within. Presto SQl Query):

and regexp_like(game_name, '^((?!QA).*$^apples.*)')

No results returned

Then tried:

and regexp_like(game_name, '^apples.*(!?QA)')

This runs and returns apples but gives me QA results only when in fact I wanted to exclude those results.

Then tried:

and regexp_like(game_name, '^apples.*[^(QA)]')

This returns apples results only but includes those with string 'QA' within them.

How can I regex filter to include 'apples' but exclude any cases that contain sub string 'QA'?

Doug Fir
  • 19,971
  • 47
  • 169
  • 299

1 Answers1

0

You may use this lookahead based regex:

^(?!.*QA).*apples.*$

(?!.*QA) is the lookahead assertion that needs to be placed next to ^ to that condition is applied to entire string since it has .* before QA.

anubhava
  • 761,203
  • 64
  • 569
  • 643