0

This simple regex works well on https://regex101.com/

"(.+)"."(.+)"

enter image description here

But when I add it to a Python code like this, it cannot match the pattern

>>> import re
>>> re.compile(r'"(.+)"."(.+)"').match('CAST("category"."quantity" AS int)') is not None
False
>>>

What could be the reason? Thanks in advance.

Quinn Wynn
  • 1,334
  • 2
  • 15
  • 20

1 Answers1

1

Replace match with search. match matches from the start of a string; search matches anywhere within the string.

>>> import re
>>> re.compile(r'"(.+)"."(.+)"').search('CAST("category"."quantity" AS int)') is not None
True

(You might also want to replace (.+) with (.+?), in case your string has more than four double quotes. And though I'm not sure about the central dot character, perhaps you want an explicit \. there? Then the pattern would become

r'"(.+?)"\."(.+?)"'

which doesn't match something like 'CAST("category"+"quantity" AS "int")', or at least returns the correct matched part (true, that's not valid SQL, but to give a quick idea.))

9769953
  • 10,344
  • 3
  • 26
  • 37