For Programmers: Free Programming Magazines  


Home > Archive > AWK > May 2004 > De-reg-exp-ing a string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author De-reg-exp-ing a string
Kenny McCormack

2004-05-04, 4:36 pm

I'm looking for a good general solution to the problem of escaping all the
"magic" characters in a string. That is, so that you can do a reg exp
match against it and not have unexpected effects from the presence of magic
characters. (Yes, I know that with the problem as stated, you might ask
"Why not just use an equality test instead of a reg exp test?" Assume
there is a good reason for doing a reg exp test [think "contains"])

I'm pretty sure I've seen a good function for this, in the examples section
of either the GAWK or TAWK distributions, but I cannot find it now
(obviously, since I am posting this...).

Here's what I have; can anyone improve on this (or point me to an
"official" solution) ?

function quot(s) {
gsub(/[]*.\\[]/,"\\\\&",s)
return s
}
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com