mIRC Homepage
Posted By: whatsthedillio removing stuff from string - 02/08/05 03:27 PM
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 ^^
Posted By: Sigh Re: removing stuff from string - 02/08/05 03:43 PM
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"
Posted By: whatsthedillio Re: removing stuff from string - 02/08/05 03:49 PM
cool works nicely
thanks alot laugh
Posted By: Kelder Re: removing stuff from string - 02/08/05 08:53 PM
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
}
Posted By: FiberOPtics Re: removing stuff from string - 02/08/05 09:11 PM
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
© mIRC Discussion Forums