From WikiChip
Editing mirc/regex

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

This page supports semantic in-text annotations (e.g. "[[Is specified as::World Heritage Site]]") to build structured and queryable content provided by Semantic MediaWiki. For a comprehensive description on how to use annotations or the #ask parser function, please have a look at the getting started, in-text annotation, or inline queries help pages.

Latest revision Your text
Line 1: Line 1:
 
{{mirc title|RegEx}}
 
{{mirc title|RegEx}}
 
Regular expressions, from here on referred to as '''regex''', can be used to perform complicated pattern matching operations. Users should already be familiar with, and comfortable using, Regular expressions at this point. The [[regular expressions|Regular Expressions]] page contains more detailed information for users who are new to regex but a reminder of the syntax will be provided at the end.
 
Regular expressions, from here on referred to as '''regex''', can be used to perform complicated pattern matching operations. Users should already be familiar with, and comfortable using, Regular expressions at this point. The [[regular expressions|Regular Expressions]] page contains more detailed information for users who are new to regex but a reminder of the syntax will be provided at the end.
 
 
'''Note''': a back reference is the same as a capturing group.
 
'''Note''': a back reference is the same as a capturing group.
  
Line 11: Line 10:
 
* --with-match-limit - around 1,000,000
 
* --with-match-limit - around 1,000,000
 
* --with-match-limit-recursion - 999
 
* --with-match-limit-recursion - 999
 
The newline sequence by default is $lf.
 
  
 
mIRC has four custom modifiers for regex:
 
mIRC has four custom modifiers for regex:
Line 20: Line 17:
 
* u - Enable the (*UCP) option (makes \b and \w works for unicode letters) and the (*UTF8) option (forces the string and pattern to be interpreted as utf8).  
 
* u - Enable the (*UCP) option (makes \b and \w works for unicode letters) and the (*UTF8) option (forces the string and pattern to be interpreted as utf8).  
  
mIRC does not allow you to retrieve the full match. You can access simple captures but you cannot access named capture. Here is an alias which allow you to get the full match (made by jaytea):
+
mIRC also supports 3 custom markers inside a pattern:
 
 
<source lang="mIRC">
 
;$regexm(input,regex,Nth).pos returns the Nth full match or its position
 
alias regexm {
 
  noop $regex(full, $2, /^(?|m(.)?|(/)|^)(.*?)(?:\1(?!.*\1)(.*)$)?$/usD)
 
  noop $regex(pcre, $regmlex(full, 1, 2), /^((?:(?!\((?:(?:MARK|PRUNE|SKIP|THEN|\*(?=:))(?::[^()]*)?|ACCEPT|COMMIT)\))\(\*.*?\))*)(.*)/us)
 
  if (!$regex( , $+(/, $regmlex(pcre, 1, 1), |, $regmlex(pcre, 1, 2), /))) {
 
    echo -eagc i * $!regexm: Invalid expression ( $+ $regerrstr $+ )
 
    return
 
  }
 
  var %char, %exp
 
  while ($chr($r(2048, 55295)) isin $1) /
 
  %char = $v1
 
  var %exp = $+(m, $regmlex(full, 1, 1), $regmlex(pcre, 1, 1), (?: $+ $regmlex(pcre, 1, 2) $+ \E)(?(R)|\K), $regmlex(full, 1, 1), $regmlex(full, 1, 3))
 
  var %str = $regsubex($1, %exp, %char) .
 
  if (!$pos(%str, %char, $regex($1, $2))) {
 
    noop $regex(check, $regsubex($1, $2, %char), / %char ( %char ?)/gxu)
 
    %str = $regsubex(fix, $left(%str, -2), / %char \K/gxu, $regml(check, \n)) .
 
  }
 
  noop $regex(final, $left(%str, -2), $+(/\Q, $replacecs($regsubex($1, $2, %char), \E, \E\\E\Q, %char, \E(.*?)\Q $+ %char), \E/u))
 
  if ($prop == pos) && ($3. isnum 1- $regml(final, 0)) return $calc(1 + $regml(final, $3).pos - $3)
 
  returnex $regml(final, $3 1)
 
}</source>
 
  
 +
* \cc - will match $chr(3) (control code for the color sequence)
 +
* \co - will match $chr(15) (control code for ending a color sequence)
 +
* \cb - will match $chr(2) (control code for the bold sequence)
  
mIRC remembers up to 50 regex matches. After 50 matches, the first match is overwritten and the backreferences for that match are lost.
+
'''Note:''' mIRC remembers up to 50 regex matches. After 50 matches, the first match is overwritten and the backreferences for that match are lost.
  
 
Example:  
 
Example:  
Line 52: Line 29:
 
/(This is) (a ) pattern/
 
/(This is) (a ) pattern/
  
This represent one regular expression (or one pattern) with 2 captured backreferences.
+
This represent one regular expression (or one match) with 2 captured backreferences.
 
 
=== Sanitizing the input ===
 
 
 
If you pass value dynamically in a regex pattern, you have to escape any characters in such value that could otherwise be interpreted by the PCRE regex engine.
 
 
 
Instead of escaping a bunch of characters with \, which is supported, PCRE allows for the \Q \E construct to not interpret anything between that construct:
 
 
 
Looking for a match on a nickname such as nick[name], you could use /\b $+ \Q $+ $nick $+ \E\b/
 
 
 
This is good but still has one flaw, if $nick contains \E, it will terminate the escaping sequence and subsequent character would be interpreted by PCRE.
 
 
 
To solve the issue, you have to "escape" all the \E inside the value you're using: /b\Q $+ $replacecs($nick,\E,\E\\E\Q) $+ \E\b/
 
 
 
There's no way to escape a character inside the escaping sequence \Q\E, you have to first terminate the sequence yourself with \E, then you have to match the actual \E from the input with \\E, and then start a new escape sequence with \Q.
 
  
 
== Regex Identifiers ==
 
== Regex Identifiers ==
Line 77: Line 40:
  
 
If the /g modifier is used, that number can be greater than 1.
 
If the /g modifier is used, that number can be greater than 1.
You may see a negative value being returned if an error occured, the list below is not exhaustive, but the error ommited are the one that can only happen because of a bug in mIRC in the way it uses pcre, which should never happen in practice.
+
You may see a negative value being returned if an error occured:
 
 
* -5 - rare, the compiled pattern has an error in it, could be due to a bug in pcre or because the pattern has been overwritten
 
* -6 - memory could not be allocated
 
* -7 - memory could not be allocated (specific to retrieving capturing groups)
 
 
* -8 - you reached the maximum number of backtracks allowed, for example: $regex($str(a,4000),(a+a+)*b)
 
* -8 - you reached the maximum number of backtracks allowed, for example: $regex($str(a,4000),(a+a+)*b)
* -14 - mIRC uses this value when compiling the pattern fails but it's a valid value meaning an internal error occured as well. If this error is returned because compiling the pattern failed, {{mIRC|$regerrstr}} is set with an error string, otherwise -14 is the pcre meaning, which is more or less the same as -5 but happens elsewhere in the code.
 
 
* -21 - you reached an internal recursion limit, for example: $regex($str(a, 1000), (a)+)
 
* -21 - you reached an internal recursion limit, for example: $regex($str(a, 1000), (a)+)
* -26 - this error is returned when pcre detects a recursion loop within the pattern. Specifically, it means that either the whole pattern or a subpattern has been called recursively for the second time at the same position in the input string. Some simple patterns that might do this are detected and faulted at compile time, but more  complicated cases, in particular mutual recursions between two different subpatterns, cannot be detected until run time. ex: $regex(,((?2))((?3))((?1)))
 
  
 
=== $regml ===
 
=== $regml ===
 
<source lang=mIRC>$regml([name],N)</source>
 
<source lang=mIRC>$regml([name],N)</source>
 
''$regml'' can be used to return the Nth back-reference. It takes the optional [name] used in $regex/$regsub/$regsubex. As with all other aspects of mIRC identifier, if ''0'' is specified as the Nth reference, then the total number of backreferences is returned.
 
''$regml'' can be used to return the Nth back-reference. It takes the optional [name] used in $regex/$regsub/$regsubex. As with all other aspects of mIRC identifier, if ''0'' is specified as the Nth reference, then the total number of backreferences is returned.
 +
 +
Note that $regml returns all of the backreference made accross all matches, see {{mIRC|$regmlex) below for a way to get the captured group per match.
  
 
$regml also has a ''.pos'' property, which returns the position within the input where the capture occurred.
 
$regml also has a ''.pos'' property, which returns the position within the input where the capture occurred.
Line 97: Line 56:
  
 
'''Note''': $regml is a list of all captures accross all the matches made (/g modifier), which is often enough, but can be a problem in some cases.
 
'''Note''': $regml is a list of all captures accross all the matches made (/g modifier), which is often enough, but can be a problem in some cases.
<source lang="mIRC">//noop $regex(name, teasat, /([es])(a)/g) | echo -a $regml(name, 0) : $regml(name, 1) -- $regml(name, 2) -- $regml(name,3) -- $regml(name,4)</source>would display "4 : e -- a -- s -- a "
+
<source lang="mIRC">//noop $regex(name, teasat, /([es])(a)/g) | echo -a $regml(name, 0) : $regml(name, 1) -- $regml(name, 2) -- $regml(name,3) -- $regml(name,4)</source>would display "2 : e -- a -- s -- a "
You can now access the Nth captured group for a given match number with {{mIRC|$regmlex}}.
+
You can now access the Nth captured group for a given match number with $regmlex.
  
 
=== $regmlex ===
 
=== $regmlex ===
Line 119: Line 78:
 
This time, <subtext> is evaluated during substitution so you can use %variables and $identifiers there, they will be evaluated.
 
This time, <subtext> is evaluated during substitution so you can use %variables and $identifiers there, they will be evaluated.
  
'''Note''': You can now use $regsubex the same way as $regsub to get it to return the number of match and filling a %variable with the result, the name is required, both also supports output to a binvar: $regsubex(name,<input>,<regex>,<subtext>,%var|&binvar)
+
'''Note''': You can now use $regsubex the same way as $regsub to get it to return the number of match and filling a %variable with the result, the name is required: $regsubex(name,<input>,<regex>,<subtext>,%var)
  
 
==== Markers, $1- and Nested $regsubex calls ====
 
==== Markers, $1- and Nested $regsubex calls ====
Line 241: Line 200:
 
So how do you use the value of the marker of the outer $regsubex inside the subtext of the inner $regsubex?
 
So how do you use the value of the marker of the outer $regsubex inside the subtext of the inner $regsubex?
  
The solution is to use '''<nowiki>[[ \t ]]</nowiki>''':
+
The solution is to use <nowiki>[[ \t ]]</nowiki>:
  
 
<source lang="mIRC">$regsubex(name,abcdefcdab,/(cd)/g,$regsubex(\t,/(.)/g,$upper( [[ \t ]] )))</source>
 
<source lang="mIRC">$regsubex(name,abcdefcdab,/(cd)/g,$regsubex(\t,/(.)/g,$upper( [[ \t ]] )))</source>
Line 378: Line 337:
 
         \w    any "word" character
 
         \w    any "word" character
 
         \W    any "non-word" character
 
         \W    any "non-word" character
[[Category:mIRC|regex]]
 

Please note that all contributions to WikiChip may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see WikiChip:Copyrights for details). Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)