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.
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: