mIRC Home    About    Download    Register    News    Help

Print Thread
parsing a string )regex) #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

Re: parsing a string )regex) [Re: dassa] #202747 29/07/08 01:56 PM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
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
Re: parsing a string )regex) [Re: dassa] #202748 29/07/08 01:57 PM
Joined: Jun 2008
Posts: 48
T
Typos Offline
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.
Re: parsing a string )regex) [Re: Typos] #202751 29/07/08 02:21 PM
Joined: Dec 2002
Posts: 2,962
S
starbucks_mafia Offline
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.
Re: parsing a string )regex) [Re: Typos] #202752 29/07/08 02:27 PM
Joined: Oct 2005
Posts: 1,741
G
genius_at_work Offline
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

Re: parsing a string )regex) [Re: qwerty] #202754 29/07/08 02:52 PM
Joined: Mar 2007
Posts: 139
S
Solo1 Offline
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

Re: parsing a string )regex) [Re: Solo1] #202766 29/07/08 07:51 PM
Joined: Apr 2004
Posts: 759
M
Mpdreamz Offline
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
Re: parsing a string )regex) [Re: Mpdreamz] #202767 29/07/08 08:25 PM
Joined: Mar 2007
Posts: 139
S
Solo1 Offline
Vogon poet
Offline
Vogon poet
S
Joined: Mar 2007
Posts: 139
Thank you. Works perfectly and i needed both examples.

Re: parsing a string )regex) [Re: dassa] #202796 30/07/08 10:00 AM
Joined: Jun 2008
Posts: 48
T
Typos Offline
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.
Re: parsing a string )regex) [Re: Mpdreamz] #202803 30/07/08 03:03 PM
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

Re: parsing a string )regex) [Re: dassa] #202811 30/07/08 09:41 PM
Joined: Apr 2004
Posts: 759
M
Mpdreamz Offline
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