mIRC Home    About    Download    Register    News    Help

Print Thread
#249238 19/11/14 09:01 PM
Joined: Nov 2014
Posts: 149
J
JuanAm Offline OP
Vogon poet
OP Offline
Vogon poet
J
Joined: Nov 2014
Posts: 149
I saw the wiki but have a single example:
Quote:
$regex(a cat walked by the dog,/cat|dog/g)
;'cat' and 'dog' are matched, returns 2


But that example, I can not understand in this code:
Code:
alias YouTube {
  if ($regex($1-, /\Qyoutube.com/watch?v=\E(\w+)$/)) {
    sockclose YouTube
    sockopen YouTube www.youtube.com 80
    ; keep the video ID for later on
    sockmark YouTube $regml(1)
    echo -a The video ID is: $regml(1)
  }
  else {
    echo $color(info) -aef /YouTube: invalid youtube link
    halt
  }
}


The line:
Code:
if ($regex($1-, /\Qyoutube.com/watch?v=\E(\w+)$/)) {


a)Why \Q ?
b) Why it that signifcan this: \E(\w+)$/)
c) What is the use of /g in the example above of wiki?

Please explain this whole line as simply and clearly, note that not mastered the English language.

Thank you.

Joined: Dec 2013
Posts: 779
N
Hoopy frood
Offline
Hoopy frood
N
Joined: Dec 2013
Posts: 779
Explaining how regular expressions work is outside the scope of this forum. There's too much to say about it that it can't be done in here.

You can read up on regular expressions at these sites:
http://www.regular-expressions.info/
http://en.wikichip.org/wiki/mirc/regex


Nillens @ irc.twitch.tv
Nillen @ irc.rizon.net
Joined: Jul 2006
Posts: 4,145
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,145
\Q \E is a sequence which allow you to escape whatever is inside this sequence, in this example it is used to escape the dot '.', the forward slash '/' and the '?' question mark, a backward slash could have been used to escape these: /youtube\.com\/watch\?v=(\w+)$/

the /g modifier allows you to multiple match at once, that's why you get 2 in your cat|dog example, if you don't use /g, once one match has been found (cat), nothing else is processed and the function would return 1.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Nov 2014
Posts: 149
J
JuanAm Offline OP
Vogon poet
OP Offline
Vogon poet
J
Joined: Nov 2014
Posts: 149
Originally Posted By: Nillen
Explaining how regular expressions work is outside the scope of this forum. There's too much to say about it that it can't be done in here.
You can read up on regular expressions at these sites:
http://www.regular-expressions.info/
http://en.wikichip.org/wiki/mirc/regex

Thanks for the information, I will come to read.


Originally Posted By: Wims
\Q \E is a sequence which allow you to escape whatever is inside this sequence, in this example it is used to escape the dot '.', the forward slash '/' and the '?' question mark, a backward slash could have been used to escape these: /youtube\.com\/watch\?v=(\w+)$/

the /g modifier allows you to multiple match at once, that's why you get 2 in your cat|dog example, if you don't use /g, once one match has been found (cat), nothing else is processed and the function would return 1.

Thanks for the explanation, it was very helpful.


Link Copied to Clipboard