Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regsub inside braces only 1

Status
Not open for further replies.

mark2atsafe

Instructor
Apr 11, 2006
2
CA
I have a string, for example...

{attr 1} {a b c} attr2 abcd attr3 {a b c}

I need to replace all spaces with special characters (say !!!) but ONLY those spaces inside braces.

So in the above I need...

{attr!!!1} {a!!!b!!!c} attr2 abcd attr3 {a!!!b!!!c}

Any suggestions? At the moment I'm using...

Code:
while {[regexp (\{)+(.)+( ) $_string] !=0} {
regsub -all (\{)+(\[^ \])+( ) $_string {&!!!} _string2
}

...the problem being that on the second pass the regexp matches with "{attr!!!1} " giving "{attr!!!1}!!!{a!!!....."

What I need is a function that says regsub (but only up to the next } char)! Is there such a component in regsub? And is it possible to do all this inside a single command, without using the loop?

Any help would be greatly appreciated, since I've been banging my head on the wall over this one for ages.

Thanks!

Mark I

PS - What I'm actually looking for is an output of...

attr 1,a b c,attr2,abcd,attr3,a b c

...but if I can flag the correct space characters I can work out the rest OK. Of course, if there is an all in one solution, so much the better.
 
Hah! I knew there'd be a simpler solution. Now you can see why I'm not a programmer! Many thanks; this is an excellent solution.

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top