0

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. . -> \.).

2 Answers 2

4
print '.{0,3}'.join(re.escape(part) for part in s.split('?')) + '.*'
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

ah 'escape' is the word, i was thinking 'quote'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.