Claudiu
Claudiu

Reputation: 229541

python+postgresql: Convert string literal to regex

I want to execute a query where I match a column name using a regex to my variable, s. s is a string literal and may contain things like ?, *, ., etc. I want to convert s so that ? turns into .{0,3}, so that all of its remaining regex symbols remain literals, and then add .* to the end. examples:

Hi -> Hi.*
Fo?ar -> Fo.{0,3}ar.*
F.a*rx -> F\.a\*rx.*
F.a?*rx -> F\.a.{0,3}\*rx

What's the best way to do this? Mostly I'm unsure how to quote a string literal so the regex symbols aren't interpreted as regex symbols (e.g. . -> \.).

Upvotes: 0

Views: 1720

Answers (2)

Mike Lewis
Mike Lewis

Reputation: 64177

Use the re.escape(string) method.

Which states:

Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.

Check out the re module page.

Upvotes: 1

nosklo
nosklo

Reputation: 223102

print '.{0,3}'.join(re.escape(part) for part in s.split('?')) + '.*'

Upvotes: 4

Related Questions