mIRC Home    About    Download    Register    News    Help

Print Thread
#202746 29/07/08 01:03 PM
Joined: Jan 2008
Posts: 22
D
dassa Offline OP
Ameglian cow
OP Offline
Ameglian cow
D
Joined: Jan 2008
Posts: 22
Hi
What is the most efficient way of getting just a number from a string. For example this123.4,5is67a8string.9 i would like to get 123456789 out of the above string. It should remove all other chars but the numbers.
I know there are other ways of doing this but its a regex am after.

Thanks kindly

Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
Try this
Code:
//var %string = this123.4,5is67a8string.9 | echo -ag $regsubex(%string,/\D+/g,)


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Joined: Jun 2008
Posts: 48
T
Ameglian cow
Offline
Ameglian cow
T
Joined: Jun 2008
Posts: 48
Put the string of text into a variable or tokenize it since it has commas in it and then run it through $regsub or $regsubex.

I am just learning regex so somthing this simple is right up my lane. lol
Enter the following line in mirc.
Code:
//tokenize 32 this123.4,5is67a8s@tr-ing.9,7y4h4,87,e7&64t.48r.647e,8ur5.,76y.t56f,4t,fy4f5r,f4.y5.f4y5,f4y.ef4ye4f | noop $regsub($1,/[a-z]/ig,,%tmptxt) | echo -a %tmptxt

What it does is uses $regsub and basically tells it to subsitute all letters with nothing.

If your wondering, the "ig" after the "/" at the end of the regex tell it that the regex is not case sensative and is greedy.

This seemed to work fine for me.

Hope it helps.

*Edit* I just noticed qwertys post that he was making at the same time I was making mine. Obviously his is a better regex but they both actually work the same. He just did it right and I did it a little silly.

Last edited by Typos; 29/07/08 02:12 PM.

I've gone to look for myself. If I should return before I get back, please keep me here.
Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
Yours only removes letters, leaving any other non-number characters in the text.

Also when using $regsub() you should generally declare the receiving variable (%tmptxt in this case) as local with var beforehand, otherwise you leave a global variable behind.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
Did you try your regex example? It doesn't do what the OP asked for. He wants only the numbers 0-9 extracted from his string. Yours removes a-z, but leaves anything that isn't a-z, which includes commas, periods, and all other punctuation and non-printing characters. qwerty's regex uses the built-in \D which is equivalent to [^0-9] (match anything that isn't (^) a number (0-9) ).

(Also, the g means global. In that the regex searches repeatedly, starting at the end of the last match, trying to find as many matches as possible before the end of the string.)

-genius_at_work

Joined: Mar 2007
Posts: 139
S
Vogon poet
Offline
Vogon poet
S
Joined: Mar 2007
Posts: 139
Hi i was looking for something similar but removes just chars a-z is there a good short way of doing this

Joined: Apr 2004
Posts: 759
M
Hoopy frood
Offline
Hoopy frood
M
Joined: Apr 2004
Posts: 759
Code:
//var %string = this123.4,5is67Aa8string.9 | echo -ag $regsubex(%string,/[a-z]/g,)


Do note that this doesn't remove upercase letters, which you didn't explicitly asked for i know.

Code:
//var %string = this123.4,5is67Aa8string.9 | echo -ag $regsubex(%string,/[a-z]/gi,)


This will, in case you do need it to remove A-Z as well.


$maybe
Joined: Mar 2007
Posts: 139
S
Vogon poet
Offline
Vogon poet
S
Joined: Mar 2007
Posts: 139
Thank you. Works perfectly and i needed both examples.

Joined: Jun 2008
Posts: 48
T
Ameglian cow
Offline
Ameglian cow
T
Joined: Jun 2008
Posts: 48
Yeah, I totally misunderstoond the OP. It's like I didnt even read it.

For some reason when I was making that string I provided I thought he wanted just the letters pulled but reading the post thats obviously not the case.

I have no idea what I was thinking tbh.

As far as the /g meaning global, thats news to me, I just thought it meant greedy because I was told so by:
Quote:
Tags/Options
There are several options or tags that can be used in regex, including case insensitivity and ignoring mirc colour codes.
/i at the end of an expression will make that expression case INsensitive, as opposede to the default which is case sensitive
/S at the end of an expression will ignore mIRC formatting codes in the expression.
/g makes the entire expression greedy. By default a regex expression in mirc will stop looking as soon as it matches something (and as such will only ever return 1 match, not very usefull alot of the time). Using the /g tag forces the expression to keep looking until its checked the entire string

from one of the many regex tutorials I use which is at http://forum.swiftirc.net/viewtopic.php?t=1250
and besides, the definition of greedy being exactly what it does and the fact that greedy is such a commonly used word in regex make it a great canidate for what the /g could have meant.

If your sure it doesnt mean greedy tho I'll take your word for it because you surely would know befoe I would because I'm just learning regex.

Anyhow, I'm going to definately be more careful when posting messages when I'm as tired as I was lastnight when I posted my previous message.



I've gone to look for myself. If I should return before I get back, please keep me here.
Joined: Jan 2008
Posts: 22
D
dassa Offline OP
Ameglian cow
OP Offline
Ameglian cow
D
Joined: Jan 2008
Posts: 22
Hi people
Thanks for all your replies. One final query, is there a way of getting just the chars a-z out of a string removing all none a-z characters?

Thanks

Joined: Apr 2004
Posts: 759
M
Hoopy frood
Offline
Hoopy frood
M
Joined: Apr 2004
Posts: 759
The answer is but a char away smile

//var %string = this123.4,5is67Aa8string.9 | echo -ag $regsubex(%string,/[^a-z]/g,)


$maybe

Link Copied to Clipboard