[Function] - RegExEsc() - Escape RegEx Special Characters Automatically!

Post your working scripts, libraries and tools for AHK v1.1 and older
User
Posts: 407
Joined: 26 Jun 2017, 08:12

[Function] - RegExEsc() - Escape RegEx Special Characters Automatically!

29 Nov 2018, 19:14

Code: Select all

String := "\.*?+[{|()^$  \E"

msgbox, % ""
. "Test: "   RegExReplace(String, "\.*?+[{|()^$  \E", "@")                                 "`r`n"
. "`r`n"
. "Test: "   RegExReplace(String, RegExEsc("\.*?+[{|()^$  \E"), "@")                       "`r`n"
. "`r`n"
. "Test: "   RegExReplace(String, RegExEsc("\.*?+[{|()^$  \E"), "$1$2$3")                  "`r`n"
. "`r`n"
. "Test: "   RegExReplace(String, RegExEsc("\.*?+[{|()^$  \E"), RegExEsc("$1$2$3", "$") )  "`r`n"


RegExEsc(String, Options := "")		;_________ RegExEsc(Function) - v1.0 __________
{

if (Options == "$")
return, RegExReplace(String, "\$", "$$$$")	;to be used with "RegExReplace" third parameter! ("$$" represents one literal "$")

return, "\E\Q" RegExReplace(String, "\\E", "\E\\E\Q") "\E"	;to be used with "RegExMatch" and "RegExReplace" second parameters! ("\\" represents one literal "\")

}
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: [Function] - RegExEsc() - Escape RegEx Special Characters Automatically!

29 Nov 2018, 21:15

It looks like something i could really use, but better examples could really be handy to showcase use-cases. And by better i mean like a sentence with punctuation... Yea,i'm not really handy with regex,i always use a regex generator...
live ? long & prosper : regards
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Function] - RegExEsc() - Escape RegEx Special Characters Automatically!

30 Dec 2018, 19:00

carno wrote:
30 Dec 2018, 16:22
Please offer a few more examples. ;)

Code: Select all

String := "Lalalallal  - \.*?+[{|()^$  \E - Lululululuulululu"

	;from string above, I want to replace "\.*?+[{|()^$  \E" with "@"
	;"\.*?+[{|()^$  \E" are regex special characters\string
	;you have to escape each one with "\" or by using "\Q"

	;I want to replace "\.*?+[{|()^$  \E" with "$1$2$3"
	;"$" is a special character for "RegExReplace()" third parameter
	;you have to escape each one with another "$"
	
	;So, you can use "RegExEsc()" to escape regex apecial characters\string for you automatically

msgbox, % "" 
. RegExReplace(String, "\.*?+[{|()^$  \E", "@")                                  " (Does not work)"    "`r`n"
. "`r`n"
. RegExReplace(String, RegExEsc("\.*?+[{|()^$  \E"), "@")                        " (works)"            "`r`n"
. "`r`n"
. RegExReplace(String, RegExEsc("\.*?+[{|()^$  \E"), "$1$2$3")                   " (Does not work)"    "`r`n"
. "`r`n"
. RegExReplace(String, RegExEsc("\.*?+[{|()^$  \E"), RegExEsc("$1$2$3", "$") )   " (works)"            "`r`n"


RegExEsc(String, Options := "")		;_________ RegExEsc(Function) - v1.0 __________
{

if (Options == "$")
return, RegExReplace(String, "\$", "$$$$")	;to be used with "RegExReplace" third parameter! ("$$" represents one literal "$")

return, "\E\Q" RegExReplace(String, "\\E", "\E\\E\Q") "\E"	;to be used with "RegExMatch" and "RegExReplace" second parameters! ("\\" represents one literal "\")

}
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Function] - RegExEsc() - Escape RegEx Special Characters Automatically!

30 Dec 2018, 19:26

Code: Select all

String := "Lalalallal  - .. \E - Lululululuulululu"

	;from string above, I want to replace ".. \E" with "@"
	;"\.*?+[{|()^$  \E" are regex special characters\string
	;you have to escape each one with "\" or by using "\Q"

	;I want to replace ".. \E" with "$1$2$3"
	;"$" is a special character for "RegExReplace()" third parameter
	;you have to escape each one with another "$"
	
	;So, you can use "RegExEsc()" to escape regex special characters\string for you automatically

Match := ".. \E"

Rep := "$1$2$3"

msgbox, % "" 
. RegExReplace(String, "\Q" Match, "@")                        " (Does not work)"    "`r`n"
. "`r`n"
. RegExReplace(String, RegExEsc(Match), "@")                   " (works)"            "`r`n"
. "`r`n"
. RegExReplace(String, RegExEsc(Match), Rep)                   " (Does not work)"    "`r`n"
. "`r`n"
. RegExReplace(String, RegExEsc(Match), RegExEsc(Rep, "$") )   " (works)"            "`r`n"


RegExEsc(String, Options := "")		;_________ RegExEsc(Function) - v1.0 __________
{

if (Options == "$")
return, RegExReplace(String, "\$", "$$$$")	;to be used with "RegExReplace" third parameter! ("$$" represents one literal "$")

return, "\E\Q" RegExReplace(String, "\\E", "\E\\E\Q") "\E"	;to be used with "RegExMatch" and "RegExReplace" second parameters! ("\\" represents one literal "\")

}
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Function] - RegExEsc() - Escape RegEx Special Characters Automatically!

30 Dec 2018, 20:08

Code: Select all

String := ".. \E - Lalalallal  - .. \E - Lululululuulululu - .. \E"

	;from string above, I want to replace only ".. \E" at the start of the string with "@"
	;"\.*?+[{|()^$  \E" are regex special characters\string
	;you have to escape each one with "\" or by using "\Q"

	;I want to replace only ".. \E" at the start of the string with "$1$2$3"
	;"$" is a special character for "RegExReplace()" third parameter
	;you have to escape each one with another "$"
	
	;So, you can use "RegExEsc()" to escape regex special characters\string for you automatically

Match := ".. \E"

Rep := "$1$2$3"

msgbox, % "" 
. RegExReplace(String, "^\Q" Match, "@")                           "   (Does not work)"    "`r`n"
. "`r`n"
. RegExReplace(String, "^" RegExEsc(Match), "@")                   "   (works)"            "`r`n"
. "`r`n"
. RegExReplace(String, "^" RegExEsc(Match), Rep)                   "   (Does not work)"    "`r`n"
. "`r`n"
. RegExReplace(String, "^" RegExEsc(Match), RegExEsc(Rep, "$") )   "   (works)"            "`r`n"


RegExEsc(String, Options := "")		;_________ RegExEsc(Function) - v1.0 __________
{

if (Options == "$")
return, RegExReplace(String, "\$", "$$$$")	;to be used with "RegExReplace" third parameter! ("$$" represents one literal "$")

return, "\E\Q" RegExReplace(String, "\\E", "\E\\E\Q") "\E"	;to be used with "RegExMatch" and "RegExReplace" second parameters! ("\\" represents one literal "\")

}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 225 guests