From WikiChip
mirc/regex
< mirc
Revision as of 22:07, 21 September 2014 by Ouims (talk | contribs) (Informations)

Template:mIRC Guide Regular expressions can be used to perform complicated pattern matching operations. You should already know how to use Regular expressions as this page won't teach them.

Informations

mIRC uses the PCRE library to implement regex with the following options enabled:

  • --enable-utf8
  • --enable-unicode-properties
  • --with-match-limit - around 1,000,000
  • --with-match-limit-recursion - 999

mIRC has two custom modifier:

  • S - strips control code from the input before matching (not supported by $hfind).
  • g - perform a global matches: after one match has been found, mIRC tries to match again from the current position

mIRC remembers up to 50 regex matches. After 50 matches, the first match is overwritten.

$regex, $regsub and $regsubex can take an optional name as a parameter, to reference that call later, if you do not specify a name, mIRC use a default.

$regex([name],<input>,<regex>)

Perform a regular expression match, returns the number of matches found. Returns a negative value to indicate an error (-8 if you reach the maximum number of match allowed or -21 if you read the maximum number of recursion allowed)

mIRC remembers up to 32 captured text (backreference), you can use $regml([name],N) to returns the Nth backreference, or the total number of backreferences with N = 0

$regml() also has a .pos property, which returns the position in the input where this was captured.

//noop $regex(name,test,/[es]/g) | echo -a $regml(name,0) : $regml(name,1) -- $regml(name,2)

$regsub([name],<input>,<regex>,<subtext>,<%varname>)

Performs a regular expression match, like $regex(), and then performs a substitution using <subtext>.

Returns N, the number of substitutions made, and assigns the result to <%varname>.

//noop $regsub(name,test,/([es])/g,t) | echo -a $regml(name,0) : $regml(name,1) -- $regml(name,2)

$regsubex([name],<input>,<regex>,<subtext>)

$regsubex is a more modern version of $regsubex, it performs the match, and then the substitution, returns the result of the substitution

This time, <subtext> is evaluated during substitution and can be an identifier.

<subtext> can also contain special markers:

  • \0 - returns the number of matches
  • \n - returns the current match number
  • \t - returns the current match text (same as $regml(\n))
  • \a - returns all match items
  • \A - returns a non-spaced version of \a.
  • \1 \2 \N ... - returns the Nth backreference for the current match

mIRC internally use $N-, $1 etc to store the value of the backreferences and replace the markers accordingly:

When mIRC is going to evaluate the subtext here

$regsubex(name,test,/([es])/g,\1)

\1 becomes "$+ $1 $+"

Because of this, you cannot use the previous $N- value in the subtext.

Another thing to note is that, because the backreferences are in $1-, if you try to reference \N and there was no backreference captured for that number for that match, the next backreference is used.

/filter

/filter supports the -g switch to use a regular expression, you cannot get the backreference value using $regml() if you use a custom alias as the output (-k), you need to use a $regex call on that line.

$hfind

$hfind can be used with regex, it doesn't support the custom S modifier

/write, $read, $fline etc

They are various places in which regex can be used.