简体   繁体   English

如何转义字符串中的特殊正则表达式字符?

[英]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:我通常使用re.findall(p, text)来匹配模式,但现在我遇到了一个问题:

I just want p to be matched as a normal string, not regex.我只想将p作为普通字符串进行匹配,而不是正则表达式。

For example: p may contain '+' or '*', I don't want these characters have special meanings as in regex.例如:p 可能包含 '+' 或 '*',我不希望这些字符在正则表达式中具有特殊含义。 In another word, I want p to be matched character by character.换句话说,我希望 p 逐字符匹配。

In this case p is unknown to me, so I can't add '\' into it to ignore special character.在这种情况下p对我来说是未知的,所以我不能在其中添加 '\' 来忽略特殊字符。

You can use re.escape :您可以使用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是否在另一个字符串中:

>>> 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:顺便说一句,如果您想将p嵌入到适当的正则表达式中,这主要有用:

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

If you don't need a regex, and just want to test if the pattern is a substring of the string, use:如果您不需要正则表达式,而只想测试模式是否为字符串的 substring,请使用:

if pattern in string:

If you want to test at the start or end of the string:如果要在字符串的开头或结尾进行测试:

if string.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 :如果您需要知道 substring 在字符串中的所有位置,请使用str.find

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

You can use .find on strings.您可以在字符串上使用.find This returns the index of the first occurence of the "needle" string (or -1 if it's not found).这将返回“needle”字符串第一次出现的索引(如果未找到则返回-1 )。 eg例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM