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. | ||
− | |||
− | |||
== 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 9: | ||
* --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 16: | ||
* 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) | ||
− | 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 28: | ||
/(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 39: | ||
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 53: | ||
'''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 75: | ||
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 81: | ||
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 197: | ||
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 239: | ||
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 257: | ||
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 270: | ||
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 | |
− | + | * \cx where x is any ascii character. The precise effect of \cx on ASCII characters is as follows: if x is a lower case letter, it is converted to upper case. Then bit 6 of the character (hex 40) is inverted. Thus \cA to \cZ become hex 01 to hex 1A (A is 41, Z is 5A), but \c{ becomes hex 3B ({ is 7B), and \c; becomes hex 7B (; is 3B). If the data item (byte or 16-bit value) following \c has a value greater than 127, a compile-time error occurs. This locks out non-ASCII characters. | |
− | + | * \e escape character, ascii 27 | |
− | + | * \f form feed character (hex 0C) | |
− | + | * \n linefeed (hex 0A) | |
− | + | * \r carriage return (hex 0D) | |
− | + | * \t tab (hex 09) | |
− | \ | + | * \0dd character with octal code 0dd |
− | \ | + | * \ddd character with octal code ddd, or back reference |
− | \ | + | * \o{ddd..} character with octal code ddd.. |
− | + | \xhh character with hex code hh | |
+ | \x{hhh..} character with hex code hhh.. (non-JavaScript mode) | ||
+ | \uhhhh character with hex code hhhh (JavaScript mode only) |