Question

Suppose I have a string like

x = "spam ?and eggs"

And I'm trying to match that "?and". Currently I'm doing it like this:

>>> print re.findall(re.escape('?and'), x)
['?and']

Is this the correct use case for re.escape? Will it work with any other string literal I'm looking for which may have other types of regex syntax in it?

My use case is with the argument in pexpect.spawn.expect(pattern), where the input pattern can be a string type which gets compiled into a regex. In some cases what I'm searching for might look like an regex but it is actually a string literal I want to match.

Was it helpful?

Solution

For pexpect, you can use expect_exact() instead of expect() to disable the regex functionality, and it will match exactly the python string you give it.

From the docs:

expect_exact(self, pattern_list, timeout=-1, searchwindowsize=-1)
This is similar to expect(), but uses plain string matching instead of compiled regular expressions in 'pattern_list'. The 'pattern_list' may be a string; a list or other sequence of strings; or TIMEOUT and EOF.

This call might be faster than expect() for two reasons: string searching is faster than RE matching and it is possible to limit the search to just the end of the input buffer.

This method is also useful when you don't want to have to worry about escaping regular expression characters that you want to match.

OTHER TIPS

Yes, that is exactly the correct use-case for re.escapethe documentation says it's "useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it" — though in your first example I think it's a bit simpler to escape the question-mark yourself, using any of these:

re.findall(r'\?and', x)          # \? in a raw string literal
re.findall('\\?and', x)          # \? in a non-raw string literal, so, \\?
re.findall('[?]and', x)          # "cheat" by using a character class

Yup, it looks right to me. If you're escaping your whole pattern, it's usually a good indication you should be using find without regex.

x.find('?and')

It gives -1 or the position. So...

>>> if x.find('?and') != -1: 
...   print "Match!"
... 
Match!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top