mIRC Home    About    Download    Register    News    Help

Print Thread
Need help with regex. #269005 26/05/21 07:18 PM
Joined: Jan 2021
Posts: 12
T
turbosmurfen Offline OP
Pikka bird
OP Offline
Pikka bird
T
Joined: Jan 2021
Posts: 12
I have made a regex which should work in most languages. But it seems like mIRC don't like it.

Let's just get this random line which I'm testing against:
Code
Vestibulum ante ipsum primis in faucibus orci https://www.youtube.com/watch?v=5gGjRcGdELs ipsum primis in faucibus orci luctus et ultrices posuere


And the working Regex for most languages:
Code
https:\/\/www\.youtube\.com\/watch\?v=[0-9a-zA-Z-_]{6,20}


I guess this Regex is not working in mIRC. If someone know how I should do this in Regex. Please tell me what I do wrong and how the Regex you made works.
I know regex but for mIRC it seems special.

Re: Need help with regex. [Re: turbosmurfen] #269009 27/05/21 09:18 AM
Joined: Jan 2004
Posts: 1,585
maroon Offline
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2004
Posts: 1,585
If you have a comma in your text or pattern, you need to either place it into a %variable or else use string1 $+ $chr(44) $+ string2 etc.

//var %msg Vestibulum ante ipsum primis in faucibus orci https://www.youtube.com/watch?v=5gGjRcGdELs ipsum primis in faucibus orci luctus et ultrices posuere , %pattern (https:\/\/www\.youtube\.com\/watch\?v=[0-9a-zA-Z-_]{6,20}) | echo -a matches: $regex(%msg,%pattern) 1st capture group: $regml(1)

Re: Need help with regex. [Re: maroon] #269047 30/05/21 05:44 PM
Joined: Jan 2021
Posts: 12
T
turbosmurfen Offline OP
Pikka bird
OP Offline
Pikka bird
T
Joined: Jan 2021
Posts: 12
Originally Posted by maroon
If you have a comma in your text or pattern, you need to either place it into a %variable or else use string1 $+ $chr(44) $+ string2 etc.

//var %msg Vestibulum ante ipsum primis in faucibus orci https://www.youtube.com/watch?v=5gGjRcGdELs ipsum primis in faucibus orci luctus et ultrices posuere , %pattern (https:\/\/www\.youtube\.com\/watch\?v=[0-9a-zA-Z-_]{6,20}) | echo -a matches: $regex(%msg,%pattern) 1st capture group: $regml(1)


I don't see much changed only the "(" and ")" was added. Can you tell me why they needed to be added to mIRC Regexp?

Re: Need help with regex. [Re: turbosmurfen] #269055 30/05/21 10:36 PM
Joined: Jan 2004
Posts: 1,322
L
Loki12583 Online
Hoopy frood
Online
Hoopy frood
L
Joined: Jan 2004
Posts: 1,322
The regex doesn't need to change at all. What matters is you store it in a a variable. You can't use the string directly in $regex when it contain a comma because mIRC uses a comma to delineate parameters.

Re: Need help with regex. [Re: turbosmurfen] #269056 30/05/21 10:50 PM
Joined: Jan 2004
Posts: 1,585
maroon Offline
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2004
Posts: 1,585
The parenthesis are needed if you want to have a capture group which allows you to return the actual url. Without the parenthesis, $regex can only return a 1 or 0 to indicate yes/no whether a url is present. (with /pattern/g it can return greater than 1).

However, because your pattern has the {6,20} containing the comma, mIRC thinks the literal comma indicates another parameter, so it either does something you don't want it to do, or else it errors because of thinking there's too many parameters. For example, the next example tests a string to see if it's base64. The 1st one returns 0 because the literal comma in {2,} confuses $regex. However the 2nd one works fine because the literal comma is placed into the %variable, and so $regex doesn't see the literal comma as an indicator of an extra parameter.

//var %string deadbeef | echo -a $regex(%string,/^[a-zA-Z0-9\/+]{2,}$/)
//var %pattern /^[a-zA-Z0-9\/+]{2,}$/ , %string deadbeef | echo -a $regex(%string,%pattern)

If you don't want to park your pattern into a %variable, you'd need to use $chr(44) like:

//var %string deadbeef | echo -a $regex(%string,/^[a-zA-Z0-9\/+]{2 $+ $chr(44) $+ }$/)

Re: Need help with regex. [Re: turbosmurfen] #269058 31/05/21 01:34 AM
Joined: Apr 2010
Posts: 966
F
FroggieDaFrog Offline
Hoopy frood
Offline
Hoopy frood
F
Joined: Apr 2010
Posts: 966
A small example of using regex within mIRC:

Code
var %test_text = Vestibulum ante ipsum primis in faucibus orci https://www.youtube.com/watch?v=5gGjRcGdELs ipsum primis in faucibus orci luctus et ultrices posuere https://www.youtube.com/watch?v=dQw4w9WgXcQ

;; Note: \Q...\E escapes an entire block of a regex pattern so individual characters do not need to be escaped
var %pattern = /(\Qhttps://www.youtube.com/watch?v=\E[0-9a-zA-Z-_]{6,20})/ig

;; Test if the text has a yt link: $regex()
;; This also returns the number of times the regex matched in the string
echo -a HasYouTubeLink: $regex(hasytlink, %test_text, %pattern)

;; Get the number of matches
echo -a Number of Youtube Links: $regml(hasytlink, 0)

;; Get first match
echo -a First Link: $regml(hasytlink, 1)

;; Get second link
echo -a Second Link: $regml(hasytlink, 2)


I am SReject
My Stuff
Re: Need help with regex. [Re: FroggieDaFrog] #269083 02/06/21 03:42 PM
Joined: Jan 2021
Posts: 12
T
turbosmurfen Offline OP
Pikka bird
OP Offline
Pikka bird
T
Joined: Jan 2021
Posts: 12
Thanks for the information about regex.