regex.try

function

Library: Regular expressions (OMREGEX)
Import : omregex.xmd

Returns: Success or failure


Declaration
export overloaded switch function
   try      value regexp      pattern
       into value markup sink matched optional
      

Argument definitions

pattern
a regular expression pattern compiled by parse
matched
destination for marked up input


Purpose

Use try to try matching an input string against a regular expression. The regular expression must first be parsed using the function parse.

You can use the function in an arbitrary pattern context like you would any other pattern matching function:

  import "beta/omregex.xmd" prefixed by regex.
  
  process
     local regex.regexp number-pattern initial { regex.parse "[1-9][0-9]*" }
  
     repeat scan "Assume 10*10=100"
     match regex.try number-pattern => num
        output num || "%n"
     match any
     again
          

If your intention is to mark up the input, you can manually match and re-emit the relevant parts of the input in this way together with the markup events. Alternatively, you can parenthesize the interesting parts of the regular expression and have regex.try emit the marked-up input into the matched parameter sink:

  import "beta/omregex.xmd" prefixed by regex.
  import "omxmlwrite.xmd"   prefixed by xml.
  
  process
     local regex.regexp number-pattern initial { regex.parse "(([1-9][0-9]*)|[^0-9]+)*" }
  
     assert "Assume 10*10=100" matches regex.try number-pattern into xml.writer into #main-output
          

The output of the above example will be

  <1 occurrence="1">Assume</1>
  <1 occurrence="2"><2 occurrence="1">10</2></1>
  <1 occurrence="3">*</1>
  <1 occurrence="4"><2 occurrence="2">10</2></1>
  <1 occurrence="5">=</1>
  <1 occurrence="6"><2 occurrence="3">100</2></1>

The element named 1 corresponds to the outer parenthesized regular expression, and the element named 2 to the inner one. To improve the output, you can also specify the element name in curly braces immediately after the opening parenthesis. If you don't want any element for a particular parenthesized group, specify no name. The following example thus outputs

            Assume <num occurrence="1">10</num>*<num occurrence="2">10</num>=<num occurrence="3">100</num>
with two simple changes:
  process
     local regex.regexp number-pattern initial { regex.parse "({}({num}[1-9][0-9]*)|[^0-9]+)*" }
  
     assert "Assume 10*10=100" matches regex.try number-pattern into xml.writer into #main-output
          

The matched output can also be processed further using do markup-parse and element rules.

Other Library Functions