Hello.
How do I escape a string as a regular expression?
For example, converting "\d+" to "\\d\+".
In languages like Python, there are dedicated functions, how do I do that in PureBasic?
Any suggestions on how to do this would be appreciated.
Sincerely.
How do I escape a string as a regular expression?
Re: How do I escape a string as a regular expression?
You question is a little vague, at least to me.
Perhaps this: https://www.purebasic.com/documentation/string/escapestring.html
Perhaps this: https://www.purebasic.com/documentation/string/escapestring.html
Re: How do I escape a string as a regular expression?
Hello.
I apologize for the vagueness of my question.
I also found "EscapeString", but it can't escape a string used as a regular expression pattern.
For example, if you want to specify the string "\d+" itself as a regular expression pattern, you need to escape it as "\\d\+".
I apologize for the vagueness of my question.
I also found "EscapeString", but it can't escape a string used as a regular expression pattern.
For example, if you want to specify the string "\d+" itself as a regular expression pattern, you need to escape it as "\\d\+".
Re: How do I escape a string as a regular expression?
Hello,riku22 wrote: Wed Feb 09, 2022 3:36 am How do I escape a string as a regular expression?
For example, converting "\d+" to "\\d\+".
In languages like Python, there are dedicated functions, how do I do that in PureBasic?
Prefix string with au tilde (~) tells PB to handle escape characters.
Code: Select all
RegEx$ = ~"\\d+" ; not "\\d\+""
Debug RegEx$ ; show \d+
; With RegEx
CreateRegularExpression(0, ~"\\d+")
Code: Select all
A$ = ~"\"Hello World\""
Debug A$ ; show "Hello World"

Re: How do I escape a string as a regular expression?
Hello.
Sorry, that's not what I want to implement.
I want to perform a function similar to Python's re.escape function or PHP's preg_quote function.
Sorry, that's not what I want to implement.
I want to perform a function similar to Python's re.escape function or PHP's preg_quote function.
Re: How do I escape a string as a regular expression?
find: [][{}()*+?.\\^$|=<>#]
replace: \\$0
\Q my_regexp \E
https://www.purebasic.fr/english/viewtopic.php?t=78070
replace: \\$0
\Q my_regexp \E
https://www.purebasic.fr/english/viewtopic.php?t=78070
Re: How do I escape a string as a regular expression?
Hello.
Thank you very much.
I was able to achieve what I wanted to do.
Sincerely.
Thank you very much.
I was able to achieve what I wanted to do.
Sincerely.