5

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.

OganM
  • 2,543
  • 16
  • 33

2 Answers2

5

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.Dots\E

Other engines rarely support this syntax.

revo
  • 47,783
  • 14
  • 74
  • 117
1

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']

paulg
  • 136
  • 1
  • 4