Regular Expressions in Nim

import re, strutils

proc main() =
  # This tests whether a pattern matches a string.
  let match = "peach".match(re"p([a-z]+)ch")
  echo match

  # Above we used a string pattern directly, but for
  # other regexp tasks you'll need to compile an
  # optimized Regex object.
  let r = re"p([a-z]+)ch"

  # Many methods are available on these objects. Here's
  # a match test like we saw earlier.
  echo "peach".match(r)

  # This finds the match for the regexp.
  echo "peach punch".find(r)

  # This also finds the first match but returns the
  # start and end indexes for the match instead of the
  # matching text.
  echo "idx: ", "peach punch".findBounds(r)

  # The `captures` variant include information about
  # both the whole-pattern matches and the submatches
  # within those matches. For example this will return
  # information for both `p([a-z]+)ch` and `([a-z]+)`.
  echo "peach punch".findAll(r)

  # Similarly this will return information about the
  # indexes of matches and submatches.
  echo "peach punch".findAllBounds(r)

  # The `findAll` function applies to all
  # matches in the input, not just the first.
  echo "peach punch pinch".findAll(r)

  # Providing a non-negative integer as the second
  # argument to these functions will limit the number
  # of matches.
  echo "peach punch pinch".findAll(r, 2)

  # Our examples above had string arguments and used
  # names like `match`. We can also provide
  # string arguments directly.
  echo "peach".match(r)

  # The `re` package can also be used to replace
  # subsets of strings with other values.
  echo "a peach".replace(r, "<fruit>")

  # The `proc` variant allows you to transform matched
  # text with a given function.
  echo "a peach".replace(r, proc(m: string): string = m.toUpperAscii)

main()

This Nim code demonstrates the use of regular expressions, which are supported through the re module. Here’s a breakdown of the example:

  1. We start by importing the necessary modules: re for regular expressions and strutils for string utilities.

  2. The match function is used to test if a pattern matches a string.

  3. We compile a regular expression pattern into a Regex object for more efficient use.

  4. Various methods are demonstrated:

    • match to test for a match
    • find to find the first match
    • findBounds to get the start and end indexes of the first match
    • findAll to get all matches
    • findAllBounds to get the bounds of all matches
  5. We show how to limit the number of matches using an optional argument in findAll.

  6. String replacement is demonstrated using the replace function, both with a simple replacement and with a function to transform the matched text.

To run this program, save it as regular_expressions.nim and use the Nim compiler:

$ nim c -r regular_expressions.nim

This will compile and run the program, displaying the results of various regular expression operations.

For a complete reference on Nim regular expressions, check the re module documentation in the Nim standard library.