mIRC Homepage

Need help with regex.

Posted By: turbosmurfen

Need help with regex. - 26/05/21 07:18 PM

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.
Posted By: maroon

Re: Need help with regex. - 27/05/21 09:18 AM

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)
Posted By: turbosmurfen

Re: Need help with regex. - 30/05/21 05:44 PM

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?
Posted By: Loki12583

Re: Need help with regex. - 30/05/21 10:36 PM

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.
Posted By: maroon

Re: Need help with regex. - 30/05/21 10:50 PM

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) $+ }$/)
Posted By: FroggieDaFrog

Re: Need help with regex. - 31/05/21 01:34 AM

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)
Posted By: turbosmurfen

Re: Need help with regex. - 02/06/21 03:42 PM

Thanks for the information about regex.
© 2021 mIRC Discussion Forums