Skip to content Skip to sidebar Skip to footer

How To Escape Special Regex Characters In A String?

I use re.findall(p, text) to match a pattern generally, but now I came across a question: I just want p to be matched as a normal string, not regex. For example: p may contain '+'

Solution 1:

You can use re.escape:

>>>p = 'foo+*bar'>>>import re>>>re.escape(p)
'foo\\+\\*bar'

Or just use string operations to check if p is inside another string:

>>>p in'blablafoo+*bar123'
True
>>>'foo+*bar foo+*bar'.count(p)
2

By the way, this is mainly useful if you want to embed p into a proper regex:

>>> re.match(r'\d.*{}.*\d'.format(re.escape(p)), '1 foo+*bar 2')
<_sre.SRE_Match object at 0x7f11e83a31d0>

Solution 2:

If you don't need a regex, and just want to test if the pattern is a substring of the string, use:

if pattern instring:

If you want to test at the start or end of the string:

ifstring.startswith(pattern): # or .endswith(pattern)

See the string methods section of the docs for other string methods.

If you need to know all locations of a substring in a string, use str.find:

offsets = []
offset= string.find(pattern, 0)
while offset!=-1:
    offsets.append(offset)
    # startfrom after the location of the previous matchoffset= string.find(pattern, offset+1)

Solution 3:

You can use .find on strings. This returns the index of the first occurence of the "needle" string (or -1 if it's not found). e.g.

>>>a = 'test string 1+2*3'>>>a.find('str')
5
>>>a.find('not there')
-1
>>>a.find('1+2*')
12

Post a Comment for "How To Escape Special Regex Characters In A String?"