2

I am trying to come up with a regex that captures the word zone in both scenarios below:

txt1='cast("zone" as varchar(100)) as zoneID'
txt2='cast(zone as varchar(100)) as zoneID'

As you can see, the " is optional. sometimes it appears and sometimes it doesn't. so the regex needs to work in both scenarios.

I have tried the following:

(?<=cast\()"?zone(?=\s|")

but the output is

"zone

when I incorporate the optional quote "? inside of the positive look behind I get the following message:

(?<=cast\("?)zone(?=\s|")
look-behind requires fixed-width pattern

I think the parenthesis is causing the issue. How can I incorporate the optional quote in the positive look-behind?

chulo
  • 53
  • 8

2 Answers2

0

I'm not sure why you are using a positive look behind to begin with. You could locate zone by simply doing this:

import re

txt1='cast("zone" as varchar(100)) as zoneID'
txt2='cast(zone as varchar(100)) as zoneID'

dasearch=re.search("\(\"?(zone)\"?",txt1)

print(dasearch.group(1))

dasearch=re.search("\(\"?(zone)\"?",txt2)

print(dasearch.group(1))

With more info on why you need a positive look ahead, I'd be happy to help.

  • good point. I was thinking that if instead of using zoneid as the alias one uses zone, then it might capture both, but I only want the zone inside of the cast, which starts with `cast(` – chulo Feb 18 '23 at 03:56
0

In Python you could make use of an if clause and get the value from capture group 2.

\bcast\((")?(zone)(?(1)"|\s)

Explanation

  • \bcast\( Match cast(
  • (")? Optionally match " in group 1
  • (zone) Capture zone in group 2
  • (?(1)"|\s) If clause, if we have group 1, then match " else match a whitespace char

See a regex101 demo and a Python demo.

import re

pattern = r'\bcast\((")?(zone)(?(1)"|\s)'
strings = [
    'cast("zone" as varchar(100)) as zoneID',
    'cast(zone as varchar(100)) as zoneID',
    'cast(zone) as varchar(100)) as zoneID'
]
for s in strings:
    m = re.search(pattern, s)
    if m:
        print(f"{m.group(2)} ---> {s}")

Output

zone ---> cast("zone" as varchar(100)) as zoneID
zone ---> cast(zone as varchar(100)) as zoneID
The fourth bird
  • 154,723
  • 16
  • 55
  • 70