Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match a whole word that includes special characters in regex

Tags:

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.

like image 713
OganM Avatar asked Dec 17 '15 20:12

OganM


2 Answers

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.

like image 64
revo Avatar answered Nov 15 '22 03:11

revo


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

like image 38
paulg Avatar answered Nov 15 '22 04:11

paulg