mIRC Home    About    Download    Register    News    Help

Print Thread
removing stuff from string #126608 02/08/05 03:27 PM
Joined: Jun 2004
Posts: 291
W
whatsthedillio Offline OP
Fjord artisan
OP Offline
Fjord artisan
W
Joined: Jun 2004
Posts: 291
hi i'm looking for some way i can remove some certain things from a string...

i want {bla}hello {...}hel{bla}lo to return as hello hello
so i want to be able to remove everything inside the braces, including the braces themselves from the string...i tried a couple of regex tutorials but couldnt figure it out >.<

please help!
thanks ^^

Re: removing stuff from string #126609 02/08/05 03:43 PM
Joined: Aug 2003
Posts: 314
S
Sigh Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2003
Posts: 314
Code:
alias rem{} {
  set -ln %x $1
  while ($regsub(%x,/\{[^{]*?\}/g,,%x)) !
  return %x
}


Usage: $rem{}(string)

\{ matches a { character, the \ escapes it since { has a special meaning in regex

[^{] matches a character that isnt {, the *? following makes it match as few as possible of these characters up until the next }

The loop is so it can remove nested {}s as well, should that be necessary, e.g. "{text {text} text} text"

Re: removing stuff from string #126610 02/08/05 03:49 PM
Joined: Jun 2004
Posts: 291
W
whatsthedillio Offline OP
Fjord artisan
OP Offline
Fjord artisan
W
Joined: Jun 2004
Posts: 291
cool works nicely
thanks alot laugh

Re: removing stuff from string #126611 02/08/05 08:53 PM
Joined: Apr 2003
Posts: 701
K
Kelder Offline
Hoopy frood
Offline
Hoopy frood
K
Joined: Apr 2003
Posts: 701
Just wondering, what's the difference between var %x = $1- and set -ln %x $1- ? Actually, why the -n switch? I can't get a difference with or without the -n switch...

I didn't know about ! being a no-op command, that's one I'll be using a lot smile Mostly in the exact same context too: regex :tongue:
Apparently !! works too, but !!! gives the /! no such command error crazy

I had to think twice before I got the need for the while loop, but you can do without (yes, I finally found a use for (?R) :tongue: ) Fails if the braces are not matched...

alias rem {
set -ln %x $1
$null($regsub(%x,/\{(?:[^{}]|(?R))*?\}/g,,%x))
return %x
}

Re: removing stuff from string #126612 02/08/05 09:11 PM
Joined: Feb 2004
Posts: 2,019
FiberOPtics Offline
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
The difference is that var will evaluate simple math expressions like:

//var %a = 1 + 1 | echo -a %a

To prevent this, the n flag is used with set, and the l flag is used to make it local.

//set -ln %a 1 + 1 | echo -a %a


Gone.