|
Joined: Mar 2007
Posts: 139
Vogon poet
|
OP
Vogon poet
Joined: Mar 2007
Posts: 139 |
Hi I am trying to parse an irc server reply. How would i use a regular expression to get the nick and the nicks address in $address(nick,2) format from the line below?
:nick!nick@hhx-48524CF5.dyn.optonline.net
Thanks
|
|
|
|
Joined: Dec 2002
Posts: 2,962
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 2,962 |
If I'm understanding you correctly there's no need to use regular expressions for this.
var %address = :nick!nick@hhx-48524CF5.dyn.optonline.net
; for the nickname
$right($gettok(%address, 1, 33), -1)
; for the masked address
$mask(%address, 2)
Spelling mistakes, grammatical errors, and stupid comments are intentional.
|
|
|
|
Joined: Jun 2008
Posts: 48
Ameglian cow
|
Ameglian cow
Joined: Jun 2008
Posts: 48 |
I think he wanted regex.
I am just learning regex but what your looking for is going ro be a $regex call that uses () to tell it which two items to store in $regml's to be grabbed. For instance:
//echo -a $regex(test,:nick!nick@hhx-48524CF5.dyn.optonline.net,/:(.+)!.+@(.+)/i) $regml(test,1) $regml(test,2)
will return 1 nick hhx-48524CF5.dyn.optonline.net
As I said, I am new to regex, that when echoed works just fine but it may not when u attempt to use the idea. I just wanted to show what it is I thought the OP wanted to see.
I've gone to look for myself. If I should return before I get back, please keep me here.
|
|
|
|
Joined: Oct 2003
Posts: 3,918
Hoopy frood
|
Hoopy frood
Joined: Oct 2003
Posts: 3,918 |
It's one thing to want a specific solution, it's another not to know that there is a simpler solution. In this case I'd have to guess that the OP isn't aware that regex simply isn't necessary, so just because they say they "want regex" doesn't mean they *actually* "want regex". I would highly recommend using a simple $gettok over a complex $regex in this situation- it's not only hundreds of times more efficient but it's easier to use and maintain.
- argv[0] on EFnet #mIRC - "Life is a pointer to an integer without a cast"
|
|
|
|
Joined: Jun 2008
Posts: 48
Ameglian cow
|
Ameglian cow
Joined: Jun 2008
Posts: 48 |
Lets see here, first off, you have to be the first person I have ever heard refer to regex as inefficient. Anyhow, lets say that the OP wants the Nick "nick !*!@hhx-48524CF5.dyn.optonline.net" exactly as it looks betwen the quotes and lets assume he wants it returned from an alias. Ok, so, my way is: alias setpurge { return $regsubex(test,:nick!nick@hhx-48524CF5.dyn.optonline.net,/:(.+)!.+@(.+)/i,$regml(test,1) !*!@ $+ $regml(test,2)) } Which when using $setpurge(:nick!nick@hhx-48524CF5.dyn.optonline.net) returns was asked for: nick *!*@hhx-48524CF5.dyn.optonline.net And your way is: alias setpurge { var %address = :nick!nick@hhx-48524CF5.dyn.optonline.net return $right($gettok(%address, 1, 33), -1) $mask(%address, 2) } Which when using $setpurge(:nick!nick@hhx-48524CF5.dyn.optonline.net) also returns what was asked for: nick *!*@hhx-48524CF5.dyn.optonline.net They both work just as well as eachother. I agree $gettok is handy and so is $mask and using negative numbers with $right is a great feature but thats no reason to assume the OP didn't want what he really asked for. If anything offer them both your solution and a regex solution so they can make the choice incase they did actually mean what they said. Something tells me you just don't like regex very much or maybe your tired of seeing people throw regex at new users that aren't ready to start using it yet but I think this was one of those times when it was ok to use it. Besides, My way = One /Return and One $RegSubEx with two $regml's in it.........Yours needed One /Var, One /Return, One $mask, One $right and One $gettok. Seems to me yours could be considered a little more inefficient due to the fact that you needed one more identifier than me. Sure I used one twice but I still didn't need as many no matter how you look at it. I don't like playing the "my code is better than yours" but when someone tries telling me that $gettok is a hundred times more efficient than $regex it just strikes me as a goofy comment. They both have many many great uses and shouldn't even be compared. Anyhow, I'm bored but this is still more time than I intended to put into this message.
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
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 2,962 |
Regular expressions aren't inefficient for what they do and are capable of, however they are inefficient for simple tasks for which they're not necessary compared to simpler, more appropriate methods. The number of identifiers/variables used means nothing at all. If you want the benchmarks then here they are: benchre {
var %address = :nick!nick@hhx-48524CF5.dyn.optonline.net, %t = $iif($int($1), $v1, 1000), %i = %t, %start = $ticks
while %i {
.echo -qa $regsubex(test, %address, /:(.+)!.+@(.+)/i, $regml(test,1) *!*@ $+ $regml(test,2))
dec %i
}
var %took = $calc($ticks - %start)
echo -a Regex took $calc(%took / 1000) secs ( $+ %t iterations)
}
benchun {
var %address = :nick!nick@hhx-48524CF5.dyn.optonline.net, %t = $iif($int($1), $v1, 1000), %i = %t, %start = $ticks
while %i {
.echo -qa $regsubex(test, %address, /:(.+?)!.+?@(.+)/i, $regml(test,1) *!*@ $+ $regml(test,2))
dec %i
}
var %took = $calc($ticks - %start)
echo -a Ungreedy regex took $calc(%took / 1000) secs ( $+ %t iterations)
}
benchcv {
var %address = :nick!nick@hhx-48524CF5.dyn.optonline.net, %t = $iif($int($1), $v1, 1000), %i = %t, %start = $ticks
while %i {
.echo -qa $right($gettok(%address, 1, 33), -1) $mask(%address, 2)
dec %i
}
var %took = $calc($ticks - %start)
echo -a Conventional took $calc(%took / 1000) secs ( $+ %t iterations)
} Results: /benchre 50000Regex took 6.344 secs (50000 iterations)/benchun 50000Ungreedy regex took 6.047 secs (50000 iterations)/benchcv 50000Conventional took 3.485 secs (50000 iterations)Obviously I ran them more than once but these are representative. The 'ungreedy regex' uses ungreedy quantifiers to make the expression slightly more efficient than yours, but as you can see they still take a great deal longer than using conventional matching methods. I've only shown benchmarks to demonstrate that regular expressions are not as efficient as you think in every situation, speed isn't really all that important. It certainly isn't the main reason I chose to give the OP the other code. The fact that the non-regex code is simpler to someone who doesn't know regular expressions is a far better reason. It's easier to understand and easier to explain to someone who doesn't already understand. Giving people regular expressions when they don't know them themselves is often a bad idea - you're basically giving them a black box which they can't alter without considerable time and effort on their part. Obviously if there's no practical way round it then I'm sure they'd rather have working code they don't understand than nothing at all, but when there's another method that is, frankly, better in every way then it's silly not to give that to them. People often ask for regular expressions because they don't know them but have heard that they're the way to match text - and suddenly forget they already know half a dozen ways to match it themselves. If it turns out they really do need a regular expression for some reason they can always say so, this isn't a one-question-one-answer forum.
Spelling mistakes, grammatical errors, and stupid comments are intentional.
|
|
|
|
Joined: Oct 2005
Posts: 1,741
Hoopy frood
|
Hoopy frood
Joined: Oct 2005
Posts: 1,741 |
I think the statement that it is more efficient is based on real benchmarks of regex vs non-regex solutions (within mIRC's scripting language). Also, in my opinion, using fancy 'tricks' like you did with using $regsubex instead of simply $regex and $regml is more likely to confuse inexperienced regex users. This way is easier to understand:
alias test {
var %text = :nick!nick@hhx-48524CF5.dyn.optonline.net
noop $regex(%text,/:?(.+)!.+@(.+)/)
return $regml(1) $regml(2)
}
In the solution you posted, you should be using the \1 and \2 regex identifiers rather than $regml. -genius_at_work
|
|
|
|
Joined: Jun 2008
Posts: 48
Ameglian cow
|
Ameglian cow
Joined: Jun 2008
Posts: 48 |
Thanks for the tips on my regex genius_at_work. Like I said Im just learning regex so input like that is very handy to me.
And Starbucks_Mafia, I totally understand your point and those benchmarks were a little suprising to me. Thanks for spelling it out for me instead of taking offense to my post and just telling me to @#$# off like some people would.
All in all I learned a couple things and we can be sure by now the OP has ample solutions. lol
I've gone to look for myself. If I should return before I get back, please keep me here.
|
|
|
|
|