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. | ||
== General Information == | == General Information == | ||
− | mIRC uses the | + | mIRC uses the [[PCRE library]] to implement regex with the following options enabled: |
* --enable-utf8 | * --enable-utf8 | ||
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 | ||
− | |||
− | |||
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 | + | mIRC also supports 3 custom markers inside a pattern: |
− | + | * \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) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | '''Note:''' mIRC remembers up to 50 regex matches. After 50 matches, the first match is overwritten and the backreferences for that match are lost. | |
− | 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 | + | This represent one regular expression (or one match) with 2 captured backreferences. |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
== 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 | + | You may see a negative value being returned if an error occured: |
− | |||
− | |||
− | |||
− | |||
* -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) | ||
− | |||
* -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)+) | ||
− | |||
=== $regml === | === $regml === | ||
Line 97: | Line 54: | ||
'''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 " | + | <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 | + | You can now access the Nth captured group for a given match number with $regmlex. |
=== $regmlex === | === $regmlex === | ||
Line 119: | Line 76: | ||
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 | + | '''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 125: | Line 82: | ||
Special markers can be used inside the <subtext> parameter of $regsubex: | Special markers can be used inside the <subtext> parameter of $regsubex: | ||
− | * \0 - Returns the number of | + | * \0 - Returns the number of matches. |
* \n - Returns the current match number. | * \n - Returns the current match number. | ||
* \t - Returns the current match text (same as $regml(\n)). | * \t - Returns the current match text (same as $regml(\n)). | ||
Line 241: | Line 198: | ||
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 | + | 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 283: | Line 240: | ||
The metacharacters are as follows: | The metacharacters are as follows: | ||
− | + | * '''\''' -- general escape character with several uses | |
− | + | * '''^''' -- assert start of string (or line, in multiline mode) | |
− | + | * '''$''' -- assert end of string (or line, in multiline mode) | |
− | + | * '''.''' -- match any character except newline (by default) | |
− | + | * '''[''' -- start character class definition | |
− | + | * '''|''' -- start of alternative branch | |
− | + | * '''(''' -- start subpattern | |
− | + | * ''')''' -- end subpattern | |
− | + | * '''?''' -- extends the meaning of '(', also 0 or 1 quantifier, also "quantifier minimizer" | |
− | + | * '''*''' -- 0 or more quantifier | |
− | + | * '''+''' -- 1 or more quantifier, also "possessive quantifier" | |
− | + | * '''{''' -- start min/max quantifier | |
− | |||
=== Inside a character class === | === Inside a character class === | ||
Line 302: | Line 258: | ||
In a character class the only metacharacters are: | In a character class the only metacharacters are: | ||
+ | * '''\''' -- general escape character | ||
+ | * '''^''' -- negate the class, but only if the first character | ||
+ | * '''-''' -- indicates character range | ||
+ | * '''[''' -- POSIX character class (only if followed by POSIX syntax) | ||
+ | * ''']''' -- terminates the character class | ||
− | |||
− | |||
− | |||
− | |||
− | |||
=== The Backslash \ === | === The Backslash \ === | ||
Line 315: | Line 271: | ||
This use of backslash as an escape character applies both inside and outside character classes. | This use of backslash as an escape character applies both inside and outside character classes. | ||
− | Another usage of the backslash is to represent non printable character | + | Another usage of the backslash is to represent non printable character: |
\a Bel character, ascii 07 | \a Bel character, ascii 07 | ||
Line 353: | Line 309: | ||
Note that octal values of 100 or greater that are specified using this syntax must not be introduced by a leading zero, because no more than three octal digits are ever read. | Note that octal values of 100 or greater that are specified using this syntax must not be introduced by a leading zero, because no more than three octal digits are ever read. | ||
− | By default, after \x that is not followed by {, from zero to two | + | By default, after \x that is not followed by {, from zero to two hexa- |
− | + | decimal digits are read (letters can be in upper or lower case). Any | |
− | + | number of hexadecimal digits may appear between \x{ and }. If a charac- | |
− | + | ter other than a hexadecimal digit appears between \x{ and }, or if | |
− | + | there is no terminating }, an error occurs. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |