mIRC Home    About    Download    Register    News    Help

Print Thread
#105516 16/12/04 08:26 PM
Joined: Mar 2003
Posts: 28
P
Ameglian cow
OP Offline
Ameglian cow
P
Joined: Mar 2003
Posts: 28
Hi,

Yesterday i have try to get a specified token out of a string... But it don't work

I have $17 filled with the string, the string is [15]) i need the numbers, but the string can also be [1]). I have try with $gettok and $deltok but don't work...

Does anybody know how the fix this? Thank you!

p.s. I hope you understand me because me english is bad shocked

#105517 16/12/04 08:35 PM
Joined: Nov 2004
Posts: 16
C
Pikka bird
Offline
Pikka bird
C
Joined: Nov 2004
Posts: 16
try remove those [,]
$remove($17,$chr(91),$chr(93))

#105518 16/12/04 08:35 PM
Joined: May 2003
Posts: 79
A
Babel fish
Offline
Babel fish
A
Joined: May 2003
Posts: 79
Every case is different, we can't read your mind! We need an example in order to make something work out.

#105519 17/12/04 01:13 AM
Joined: May 2003
Posts: 79
A
Babel fish
Offline
Babel fish
A
Joined: May 2003
Posts: 79
Oops, sorry, I thought you wanted something else than what you had. So, if it's always [number], you can remove the [ and the ], as mentionned before, or you can use $left and $right, well, there's other possibilities than using the tokens for this one.

#105520 17/12/04 02:11 AM
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
try $remove($17,$chr(91),$chr(93),$chr(41)) sicne you want to remove the [,] & )

Here is a $gettok solution (its not the only token solution)
$gettok($gettok($17,1,91),1,93)

First $gettok($17,1,91) makes "[number])" become "number])" since leading tokens seperators 91=[ are dumped

Second $gettok( XXX ,1,93) makes "number])" become "number" since the first token seperator is now 93=]

#105521 17/12/04 01:09 PM
Joined: Feb 2004
Posts: 2,019
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
Hehe,

I love how you can use various styles to achieve the same thing. Since we're showing different approaches, here's another one:

Usage: $getnum(string)

alias getnum !.echo -q $regex($$1,/\[(\d+)\]/S) | return $regml(1)


Gone.
#105522 17/12/04 08:30 PM
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
The regex is nice, Im not very good at them i must admit so avoid them unless i really need one. I just said not the only one since i was sure someone was gonna go you could have used $deltok($deltok($1,2-,93),0,91) or .... or ... or ....

But the regex was very good i assume it seeks the [xxx] of [xxx]) and then takes only digits from the xxx ?

#105523 17/12/04 09:07 PM
Joined: Feb 2004
Posts: 2,019
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
Oh, but I think your gettok solution or the $remove solution is better if it's sure that the input will always be a number surrounded by [ ]. There's really no need for the regex, just wanted to show another approach.

Indeed, it captures digits inside [ and ].

More into detail: $regex($$1,/\[(\d+)\]/S)

* First part: input = $$1, double $$ so that the alias will stop if no input is specified

* Second part: the expression to match, enclosed between / /

--> the [ ] together have a special meaning inside a regex, because they define a character class. Therefore they need to be escaped, for instance by putting a \ backslash in front of them. Another way would be to enclose the string that you want escaped by putting that string between \Q and \E.

--> The brackets ( and ) are there around \d+, so that the matched string is captured, and can be referenced later on in the $regml

--> \d represents a digit 0-9, and the plus + means match 1 or more times, so here, the regex matches one or more digits. Since a regex is by default greedy, it will try to match as many numbers as it can, unless specified differently.

* Third part: the modifier, after the last slash /

--> The S modifier will strip the input string from control codes.

After calling the $regex with !.echo -q, if there is a match, there will be a reference to it in $regml(1). 1 because that is the first captured string in the expression. It could be useful to name a regex, so that references aren't overwritten by another regex call, though I didn't add it.

In a lot of cases, indeed, regex isn't really necessary, and solutions can be found with other tools.Then again, in other cases, they are absolutely indispensable, and provide an awesome tool.

Greets


Gone.

Link Copied to Clipboard