How to match a whole word that includes special characters in regex
In regex is there a way to escape special characters in an entire region of text in PCRE syntax?
eg. hey+Im+A+Single+Word+Including+The+Pluses+And.Dots
Normally to match the exact string in regex I would have to escape every single +
and .
with /
s in the above string. This means that if the string is a variable, One has to seek for special characters and escape them manually. Is there an easier way to do this by telling regex escape all special characters in a block of text?
The motivation behind this is to append this to a larger regex so even though there are easier ways to get exact matches they don't apply here.
Everything between Q
and E
meta characters are treated as literals in PERL Compatible RegExes (PCRE). So in your case:
Qhey+Im+A+Single+Word+Including+The+Pluses+And.DotsE
Other engines rarely support this syntax.
If it's python. you can use re.escape(string) to get a literals string
import re
search = 'hey+Im+A+Single+Word+Including+The+Pluses+And.Dots'
text = '''hey+Im+A+Single+Word+Including+The+Pluses+And.Dots
heyImmASingleWordIncludingThePlusessAndaDots
heyImASingleWordIncludingThePlusesAndxDots
'''
rc = re.escape(search)
#exactly first line in text
print(re.findall(rc,text))
#line two and three as it will + as repeat and . as any char
print(re.findall(search,text))
-------- result -------------------
['hey+Im+A+Single+Word+Including+The+Pluses+And.Dots'] ['heyImmASingleWordIncludingThePlusessAndaDots', 'heyImASingleWordIncludingThePlusesAndxDots']
链接地址: http://www.djcxy.com/p/77028.html上一篇: 正则表达式匹配日期