Need a regex to escape all escapable characters in a string

Need a regex to escape all escapable characters in a string

Post by Mel » Wed, 27 Jun 2007 05:37:13


I need to escape all "escapable" characters in a string.

items like:
' " [ ] } { ;

can it be done with a single regex or should it be done individually ?

thanks for your help
 
 
 

Need a regex to escape all escapable characters in a string

Post by Jeff Godfr » Wed, 27 Jun 2007 05:58:21


What's your ultimate goal? Explaining that might get you a better
solution to the actual problem. That said, I'd look to [string map]
to do what you request.

Jeff

 
 
 

Need a regex to escape all escapable characters in a string

Post by Darren Ne » Wed, 27 Jun 2007 06:13:38


It would depend which are the escapable ones, and how you want to escape
them?

--
Darren New / San Diego, CA, USA (PST)
I bet exercise equipment would be a lot more
expensive if we had evolved from starfish.
 
 
 

Need a regex to escape all escapable characters in a string

Post by Alexandre » Wed, 27 Jun 2007 06:48:42


It's unclear against what you're willing to protect them:

- Tcl dollar or command substitutions ($[])
- Tcl list element ("{}) and command (;) parsing
- Other shells' quoting (')

However, assuming you want to stay in Tcl, and in addition to more
explicit [regsub] or [string map] operations, you may find useful the
following definition and trick:

The definition: whatever dangerous compounds are in $x, [list $x] is
guaranteed to build an airtight barrier around them (technically it
just builds a proper singleton with no string representation, but as
soon as you request this string rep, it is rebuilt with nontrivial
rules).

The trick: It does so usually with a pair of {}, but occasionally it's
impossible (eg when the string ends with an odd number of
backslashes). In this case it resorts to individual "escaping". Hence:

list "$x \\"

will magically escape what needs be (and of course add the four
characters "\ \\")

-Alex
 
 
 

Need a regex to escape all escapable characters in a string

Post by Trony » Wed, 27 Jun 2007 12:20:48


Try:

regsub -all {(\[\]\}\{\;\"\'])} $string {\\\1} newstring

There may be a way to not have to escape everything in the pattern but
this seems to work...
 
 
 

Need a regex to escape all escapable characters in a string

Post by Larry W. V » Wed, 27 Jun 2007 20:17:34


Well, the quickest way to escape characters is to do something like
set a {characters to escape}

There are, however a couple of exceptions to that - if the string
contains a }, or, I think, if the last character in the string is a
\ .

If you don't feed your string to eval, you may not even need to escape
it.
 
 
 

Need a regex to escape all escapable characters in a string

Post by Mel » Thu, 28 Jun 2007 00:47:59


When I try:

set string {A ' s " and { } BBBBs}
regsub -all {(\[\]\}\{\;\"\'])} $string {\\\1} newstring
puts $newstring

I GET:
>>> A ' s " and { } BBBBs

am I doing something wrong with your regsub. There dont seem to be any
substitution done !!!

thanks for your help
 
 
 

Need a regex to escape all escapable characters in a string

Post by Bryan Oakl » Thu, 28 Jun 2007 01:06:06


You are missing a leading '['. Try this:

regsub -all {([\[\]\}\{\;\"\'])} $string {\\\1} newstring


--
Bryan Oakley
http://www.yqcomputer.com/