All pastes #70447 Raw Edit

$stripwm v1.0

public text v1 · immutable
#70447 ·published 2006-06-24 08:51 UTC
rendered paste body
$stripwm v1.0

LOAD
Copy paste this alias somewhere in your remote, preferably at the first or last line.

USAGE
Syntax: $stripwm(text,wildmatch).[frN]

text: is the text you wish to strip.

wildmatch: the wildmatch you wish to strip. The wildmatch character is "*" (without the quotes).
"a*d" will match "abcdefg" but also "1234abcdefg"
so unlike wildmatching ON TEXT "a*d" means a followed by zero or more characters up to the last d it finds anywhere in the string
and not a string that starts with a followed by 1 or more characters followed by a d.
therefor the match "a*" will completely strip the text "abcdefg"

Properties:
-f : this makes * stop at the First character it finds after * instead of the last
//echo -a$stripwm(aq hello aq,a*q).f
returns "hello" instead of stripping the entire text.
-rN : makes it strip everything but the matches it makes and returns a tokenized list of all the matches.
//echo -a$stripwm(aq hello aq hello arghq,a*q).fr
returns "aq,aq,arghq"
//echo -a $stripwm(aq hello aq hello arghq,a*q).r
returns "aq hello aq hello arghq"

N stands for the ascii delimiter for the return tokens
//echo -a $stripwm(aq hello aq hello arghq,a*q).fr46
returns "aq.aq.arghq"

if not specified it will use $chr(44) (,) by default.

In short
* means zero or more characters ! "h*e" matches "hello".
* matches the longest possible match unless you specify the .f property then it will match the shortest
.rN makes it strip everything BUT the match you specify returning a tokenized list of all the values it didnt strip.
Where N is an optional property for the ascii value of the delimiter.

Code:

alias stripwm {
  if ((!$1) || (!$2)) || (!$isid) return
  var %r = $+(/,$chr(40),$regsubex($regsubex($regsubex($regsubex($2,/(\.|\?|\+|\[|\]|\^|\$|\\)/g,\\t),/\*$/,*$),/^\*/,^*),/\*/g,$+(.*,$iif(f isin $prop,?))),$chr(41),/g)
  if (r isin $prop) {
    var %t = $regex(stripwm,$1,%r) , %m = $regml(stripwm,0) , %x = 1 , %return
    while (%x <= %m) {
      %return = $instok(%return,$regml(stripwm,%x),%x,$iif($regsubex($prop,/\D/g,),$v1,44))
      inc %x
    }
    return %return
  }
  return $regsubex($1,$+(%r,g),)
}