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{}}]