mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
ok i suck at regsub, and i could always just do a dirty big $remove but i thought someone might know a simple $regsub for it that wont look so ugly, like regex any non number and replace with nothing?

I want to remove anything besides 0123456789 from a var

example
var %value = 489.290djdue93jd93hjs923" $+ , $+ 59sj39e3333" ssss999 $crlf blah 1
(ie: anything at all)

Result i want 4892909393923593933339991

Thanks in advance guys. (and girls?)

Joined: Aug 2005
Posts: 525
S
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2005
Posts: 525
Using $regsub

Code:
alias numonly {
  var %a = $1-
  !.echo -q $regsub(%a,/[^0-9]/g,,%a)
  return %a
}


Using $regsubex

Code:
alias numonly {
  var %a = $1-
  return $regsubex(%a,/[^0-9]/g,)
}


[0-9] will match any digit, by adding the ^ it means to match anything except a digit. The /g modifier is used to find all matches in the string.

Joined: Sep 2003
Posts: 4,230
D
DaveC Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Thanks man, damn simple when u know how, i spent 15 to 20 mins on it and still couldnt do it frown LOL, damn my old brain! its run out of room for anything new like regex.

Joined: Aug 2005
Posts: 525
S
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2005
Posts: 525
Erm, sorry. If you use the $regsubex method, it can be simply:

alias numonly return $regsubex($1-,/[^0-9]/Sg,)

I just realized it should have been that way, however it will work either way. Though, in this one I added the S modifier to strip text before matching (in the off chance that your var contains colors).

If you wish to learn regex, hixxy has a pretty good tutorial: here to get you started if you haven't already looked through it.

Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
Not to nitpick, but [0-9] is \d and [^0-9] is \D .

And in this specific case, the S switch isn't necessary since all control codes would be considered "not digits" anyway.

-genius_at_work

Joined: Feb 2004
Posts: 2,019
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
To nitpick even further I'd use \D+ to remove multiple consecutive non-digits in one sweep, rather than removing them one by one.


Gone.
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
Quote:
And in this specific case, the S switch isn't necessary since all control codes would be considered "not digits" anyway.

Not necessarily true. schaefer31 clarified:
Quote:
(in the off chance that your var contains colors)

Eg $regsubex(1test2,/\D+/g,) = "12"
but $regsubex(1test2,/\D+/gS,) = "2"
So it really depends on what DaveC wants: if he wants to keep only "visible" (ie not part of a color sequence) digits, /S should be used.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Thanks everyone for the adjustments, also thanks for explaining the /S, however $regsubex($1-,/\D+/g,) is what i require for my need, the data im parsing isnt irc messages, so i doubt there would be control codes there anyway, but just as i think i said, any non numerics need dumping. (I should have thought to include a ctrl-k12,3 with output retaining 123 in it but i didnt really think about it)


Link Copied to Clipboard