问题
I use Expect
as testing framework and write some helper functions to simplify typing of matching patterns for expect
command.
So I look for function that transform any string into string in which all special regex syntax escaped (like *
, |
, +
, [
and other chars) so I would be able put any string into regex without worrying that I break regex:
expect -re "^error: [escape $str](.*)\\."
refex "^error: [escape $str](.*)\\." "lookup string..."
For expect -ex
and expect -gl
it is pretty easy to write escape function. But for expect -re
it is hard as I am newbie to TCL...
PS I write this code and currently test them:
proc reEscape {str} {
return [string map {
"]" "\\]" "[" "\\[" "{" "\\{" "}" "\\}"
"$" "\\$" "^" "\\^"
"?" "\\?" "+" "\\+" "*" "\\*"
"(" "\\(" ")" "\\)" "|" "\\|" "\\" "\\\\"
} $str]
}
puts [reEscape {[]*+?\n{}}]
回答1:
One safe strategy is to escape all non-word characters:
proc reEscape {str} {
regsub -all {\W} $str {\\&}
}
The &
will be substituted by whatever was matched in the expression.
Example
% set str {^this is (a string)+? with REGEX* |metacharacters$}
^this is (a string)+? with REGEX* |metacharacters$
% set escaped [reEscape $str]
\^this\ is\ \(a\ string\)\+\?\ with\ REGEX\*\ \|metacharacters\$
来源:https://stackoverflow.com/questions/15042826/escape-string-for-putting-it-into-regex-in-tcl