$re_parse() - Parsing mIRC Regular Expressions

By jaytea on Dec 13, 2011

/*
{

$re_parse()[.prop]

Takes in an mIRC regular expression and returns information
about it. Note the distinction between an mIRC regex and a
PCRE one: mIRC, being the application, interprets pattern
enclosing characters and modifiers. Thus the expressions that
this alias deals with are ones that are used, for example, in
$regex(), $regsub(), /filter -g, $hfind(), etc.

Properties:

.compiles - Returns $true if the pattern can be successfully
compiled by PCRE, $false otherwise.
.delimiter - Returns the pattern delimiter if one is used.
.exp - Returns the expression fed to the PCRE library,
excluding option setting verbs.
.modifiers - Returns the modifiers in the expression, if any.
.verbs - Returns the verbs at the start used to set options.
.all - Returns the name of a hash table containing results.

.all saves you having to re-call $re_parse() if you need a full
set of data. The hash table is filled with item names that match
the properties above, and the scripter should ensure to /hfree
the table when they are finished using it.

Without a property, it defaults to .compiles.

Examples:

  • $re_parse(/abc/).exp = abc
  • $re_parse(/abc[def/) = $false
  • $re_parse(/\w+/ig).modifiers = ig
  • $re_parse(m#abc#).delimiter = #

}
*/

alias re_parse {

  var %re = /(*UTF8) ^ (?| m(.?)|(/?) ) ( (?: \(\* (?:                      $&
    NO_START_OPT|CR|LF|CRLF|ANYCRLF|ANY|BSR_ANYCRLF|BSR_UNICODE|UTF8|UCP    $&
    ) \) )* ) (?| (.*)\1|(.*) ) (.*) /xs

  if (!$isutf($utfencode($1))) %re = 

  noop $regex(re_parse, $1, %re)

  if ($prop) {
    if ($prop $+ * iswm delimiter) returnex $regml(re_parse, 1)
    if ($v1 iswm verbs) return $regml(re_parse, 2)
    if ($v1 iswm exp) returnex $regml(re_parse, 3)

    var %m = [AEgimsSUxX]
    noop $regsub(re_parse2, $regml(re_parse, 4), /( %m )(*SKIP)(?= %m *\1)|.+/gsx, , %m)

    if ($v1 iswm modifiers) return %m
  }

  var %compiles = $istok($regex(,                                                  $&
    $+(/, $regml(re_parse, 2), |, $regml(re_parse, 3), /, $remove(%modifiers, g))  $&
    ), 1, 1)

  if ($v1 iswm all) {
    var %name

    ; "do/while in mIRC??" ;) just for fun
    :do {
      %name = re_parse $+ $rand(1, $not(0))
    }
    while ($hget(%name)) goto do

    hmake %name 10
    hadd %name compiles %compiles
    hadd %name delimiter $regml(re_parse, 1)
    hadd %name verbs $regml(re_parse, 2)
    hadd %name exp $regml(re_parse, 3)
    hadd %name modifiers %m

    return %name
  }

  if (!%re) return $false

  return %compiles
}

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.