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 Guide}} |
− | 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 | + | {{DISPLAYTITLE:RegEx - mIRC}} |
− | + | 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. | |
− | |||
== 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 12: | Line 11: | ||
* --with-match-limit-recursion - 999 | * --with-match-limit-recursion - 999 | ||
− | + | mIRC also has two custom modifiers for regex: | |
− | |||
− | mIRC has | ||
* S - Strips control codes from the input before matching (not supported by $hfind). | * S - Strips control codes from the input before matching (not supported by $hfind). | ||
* g - Performs global matches: after one match has been found, mIRC tries to match again from the current position. | * g - Performs global matches: after one match has been found, mIRC tries to match again from the current position. | ||
− | |||
− | |||
− | |||
− | |||
− | + | '''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 21: | ||
/(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 32: | ||
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 | |
− | + | * -21 - you reached an internal recursion limit | |
− | |||
− | |||
− | * -8 - you reached the maximum number of backtracks allowed | ||
− | |||
− | * -21 - you reached an internal recursion limit | ||
− | |||
=== $regml === | === $regml === | ||
Line 94: | Line 43: | ||
Below is an example of a regular expression, using ''name'' as the optional ''[Name]'' property, and then using $regml to reference the match(es): | Below is an example of a regular expression, using ''name'' as the optional ''[Name]'' property, and then using $regml to reference the match(es): | ||
− | <source lang="mIRC">//noop $regex(name, test, / | + | <source lang="mIRC">//noop $regex(name,test,/[es]/g) | echo -a $regml(name,0) : $regml(name,1) -- $regml(name,2)</source> |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=== $regsub === | === $regsub === | ||
− | <source lang="mIRC">$regsub([name], <input>, <regex>, <subtext>, <%varname>)</source> | + | <source lang="mIRC">$regsub([name],<input>,<regex>,<subtext>,<%varname>)</source> |
Performs a regular expression match, like $regex, and then performs a substitution using <subtext>. Returns the number of substitution made and fills <%varname> with the resulting text | Performs a regular expression match, like $regex, and then performs a substitution using <subtext>. Returns the number of substitution made and fills <%varname> with the resulting text | ||
− | <source lang="mIRC">// | + | <source lang="mIRC">//noop $regsub(name,test,/([es])/g,t) | echo -a $regml(name,0) : $regml(name,1) -- $regml(name,2)</source> |
=== $regsubex === | === $regsubex === | ||
Line 117: | Line 55: | ||
$regsubex is a more modern version of $regsub, in that it performs the match, then the substitution, and finally returns the result of the substitution. | $regsubex is a more modern version of $regsub, in that it performs the match, then the substitution, and finally returns the result of the substitution. | ||
− | 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 |
− | |||
− | |||
==== Markers, $1- and Nested $regsubex calls ==== | ==== Markers, $1- and Nested $regsubex calls ==== | ||
Line 125: | Line 61: | ||
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 146: | Line 82: | ||
* \2 is 'f' | * \2 is 'f' | ||
* \n is 2 because it's the second match. | * \n is 2 because it's the second match. | ||
− | * \t is the current matchtext (same as $regml(\n)), which is 'c' and not 'e' (\n is 2), it is important to note that when you have more than one backreference and more than one match | + | * \t is the current matchtext (same as $regml(\n)), which is 'c' and not 'e' (\n is 2), it is important to note that when you have more than one backreference and more than one match, \t is a bit meaningless. |
* \a is "e f" while \A is "ef" | * \a is "e f" while \A is "ef" | ||
Line 170: | Line 106: | ||
<source lang="mIRC">\t \t \1 \n</source> | <source lang="mIRC">\t \t \1 \n</source> | ||
mIRC assigns the value of \t to ''$1'' & ''$2'', \1 is assigned to ''$3'' and finally, \n is assigned to ''$4''. | mIRC assigns the value of \t to ''$1'' & ''$2'', \1 is assigned to ''$3'' and finally, \n is assigned to ''$4''. | ||
− | |||
− | |||
− | |||
− | |||
'''Note''': If the form '''\N''' is used in the subtext, where N is a positive number greater-than or equal to 1 (such as '''\1'''), and there is no such back-reference number in the pattern, mIRC will use the value of '''$regml(\n + N - 1)'''. | '''Note''': If the form '''\N''' is used in the subtext, where N is a positive number greater-than or equal to 1 (such as '''\1'''), and there is no such back-reference number in the pattern, mIRC will use the value of '''$regml(\n + N - 1)'''. | ||
Line 183: | Line 115: | ||
* When ''a'' is matched, '''\n''' is ''1'', and only one marker is used. Therefore, '''$1''' (used to represent \6) is filled with ''$regml(1 + 6 -1) = $regml(6)'', which is '''f''' | * When ''a'' is matched, '''\n''' is ''1'', and only one marker is used. Therefore, '''$1''' (used to represent \6) is filled with ''$regml(1 + 6 -1) = $regml(6)'', which is '''f''' | ||
* When ''b'' is matched, '''\n''' is ''2'', '''$1''' is then filled with ''$regml(2 + 6 - 1) = $regml(7)'', which is '''g''' | * When ''b'' is matched, '''\n''' is ''2'', '''$1''' is then filled with ''$regml(2 + 6 - 1) = $regml(7)'', which is '''g''' | ||
− | * And so on until '''\n + N - 1''' is greater than the | + | * And so on until '''\n + N - 1''' is greater than the number of back-references, which at this point, '''$null''' is used. |
==== Nested calls ==== | ==== Nested calls ==== | ||
Nested '''$regsubex''' calls are possible, but caution must be taken with markers. | Nested '''$regsubex''' calls are possible, but caution must be taken with markers. | ||
− | First of all, if you use the /g modifier in | + | First of all, if you use the /g modifier in the outer $regsubex and you need to use the different backreferences made inside the inner $regsubex, you must give a name to either the outer or inner $regsubex (ot both), otherwise, the call of the inner $regsubex will overwrite the backreferences of the outer $regsubex (if you don't use a name, mIRC use a default name, which would be the same here). |
When mIRC replaces the markers, it will do so on the whole subtext parameter, consider: | When mIRC replaces the markers, it will do so on the whole subtext parameter, consider: | ||
Line 195: | Line 127: | ||
In the above example, the outer '''$regsubex''' will make the regex match, then it will loop on the result and replace ''\t'' accordingly everywhere in the subtext. Notice the subtext of the outer '''$regsubex''' is: | In the above example, the outer '''$regsubex''' will make the regex match, then it will loop on the result and replace ''\t'' accordingly everywhere in the subtext. Notice the subtext of the outer '''$regsubex''' is: | ||
<source lang="mIRC">$regsubex(\t,/(.)/g,$upper(\t))</source> | <source lang="mIRC">$regsubex(\t,/(.)/g,$upper(\t))</source> | ||
− | All occurences of '''\t''' | + | All occurences of '''\t''' there are changed with corresponding $N value, even the one inside '''$upper'''; this means that the code won't work as expected. Instead, the '''\t''' inside '''$upper''' needs to be the value of the matched text of the ''inner'' '''$regsubex''', ''not'' the outer one. |
− | The idea is to get mIRC to see something other than '''\t''' | + | The idea is to get mIRC to see something other than '''\t''' when looking at this marker inside $upper, from the outer '''$regsubex''' context. |
A simple $+ cannot be used: | A simple $+ cannot be used: | ||
<source lang="mIRC">$regsubex(\t,/(.)/g,$upper( \ $+ t ))</source> | <source lang="mIRC">$regsubex(\t,/(.)/g,$upper( \ $+ t ))</source> | ||
− | Having this as the subtext of the $regsubex would end up calling '''$upper(\t)''' with plain text "\t", because the '''$+''' is going to be evaluated | + | Having this as the subtext of the $regsubex would end up calling '''$upper(\t)''' with plain text "\t", because the '''$+''' is going to be evaluated at the same time '''$upper''' is evaluated, which is after both $regsubex looked for markers. Something need to be done before the markers are replaced. |
− | The solution is to use the '''<nowiki>[[ \ $+ t ]]</nowiki>''' construct: | + | The solution to this issue is to use the '''<nowiki>[[ \ $+ t ]]</nowiki>''' construct: |
<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 211: | Line 143: | ||
The example above conveys how only the '''[[ ]]''' has changed. Remember '''$+''' was not evaluated because the subtext parameter of $regsubex is not evaluated until the regex match is performed. | The example above conveys how only the '''[[ ]]''' has changed. Remember '''$+''' was not evaluated because the subtext parameter of $regsubex is not evaluated until the regex match is performed. | ||
− | Now, the outer '''$regsubex''' is evaluated, | + | Now, the outer '''$regsubex''' is evaluated, [ \ $+ t ] produces \t right before the $regsubex gets its parameters, makes the regex match, and calls the subtext for each match: |
− | <source lang="mIRC">$regsubex(<value of \t from the outer $regsubex>,/(.)/g,$upper( | + | <source lang="mIRC">$regsubex(<value of \t from the outer $regsubex>,/(.)/g,$upper( \t ))</source> |
− | |||
− | |||
− | |||
− | |||
− | + | The more nested $regsubex you have, the more you have to makes sure each subtext has the correct number of [[ ]]. You can make this cleaner by calling a custom alias as the subtext with the markers passed as parameters and doing the nested $regsubex call here. | |
==== No Marker ==== | ==== No Marker ==== | ||
Line 226: | Line 154: | ||
<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> | ||
− | + | Two markers are used in the subtext of the outer $regsubex, so right before evaluating the subtext, $1 and $2 get filled with the value of \t (cd), the first \t is replaced with $1 in the line while the second is replaced with $2 (no evaluation): | |
− | |||
− | Two markers are used in the subtext of the outer $regsubex, so | ||
<source lang="mIRC">$regsubex($1,/(.)/g,$upper( $+ $2 $+ )))</source> | <source lang="mIRC">$regsubex($1,/(.)/g,$upper( $+ $2 $+ )))</source> | ||
− | However remember that the subtext is not evaluated before the regex match is done, so only $1 gets evaluated here: | + | However remember that the subtext is not evaluated before the regex match is done, on purpose so only $1 gets evaluated here: |
<source lang="mIRC">$regsubex(cd,/(.)/g,$upper( $+ $2 $+ )))</source> | <source lang="mIRC">$regsubex(cd,/(.)/g,$upper( $+ $2 $+ )))</source> | ||
As you can see, mIRC adds the '''$+''' if the markers have text surrounding them, that's why you don't need to space them out like identifiers. | As you can see, mIRC adds the '''$+''' if the markers have text surrounding them, that's why you don't need to space them out like identifiers. | ||
− | You might understand why it's failing at this point: $2 | + | You might understand why it's failing at this point: what's the value of $2 after the regex match is made for this inner $regsubex, when mIRC is evaluating its subtext? |
+ | Well, there is one backreference but no marker is used in this subtext, so $1- is $null here, so $2 will be evaluated as $null. | ||
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 247: | Line 174: | ||
As we saw, mIRC will first turn this line into | As we saw, mIRC will first turn this line into | ||
− | <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> |
Then the outer $regsubex will make the regex match and replaces markers so the subtext of the outer regsubex becomes: | Then the outer $regsubex will make the regex match and replaces markers so the subtext of the outer regsubex becomes: | ||
Line 253: | Line 180: | ||
<source lang="mIRC">$regsubex($1,/(.)/g,$upper( [ $+ $2 $+ ] )))</source> | <source lang="mIRC">$regsubex($1,/(.)/g,$upper( [ $+ $2 $+ ] )))</source> | ||
− | The difference is that you now have a pair of bracket, | + | The difference is that you now have a pair of bracket, each this this subtext is evaluated, the [ ] are processed first, forcing the evaluation of $2, which has the correct value at this point. |
'''Note''': the more you are nesting, the more you need to get mIRC to see the correct things, which easily gets ugly, calling an alias in the subtext to do the replacement is recommended. | '''Note''': the more you are nesting, the more you need to get mIRC to see the correct things, which easily gets ugly, calling an alias in the subtext to do the replacement is recommended. | ||
Line 261: | Line 188: | ||
== $hfind == | == $hfind == | ||
− | '''$hfind''' can be used along with '''regex'''. However, '''$hfind''' does not support the custom '''S''' modifier. | + | '''$hfind''' can be used along with '''$regex'''. However, '''$hfind''' does not support the custom '''S''' modifier. |
== /write, $read, $fline, etc == | == /write, $read, $fline, etc == | ||
These, and many more, are various places where [[regular expression|Regular Expression]] can be used. | These, and many more, are various places where [[regular expression|Regular Expression]] can be used. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |