March Docs

Regex

Regex module: simple regular expression engine (pure March implementation).

Supports: literal chars exact character match . any single character

  • zero or more of preceding atom (greedy)
+                   one or more of preceding atom (greedy)
?                   zero or one of preceding atom (greedy)
^                   anchor at start of string
$                   anchor at end of string
[abc]               character class (any of a, b, c)
[a-z]               character range in a class
[^abc]              negated character class
\d  \D              digit / non-digit
\w  \W              word char (alphanumeric + _) / non-word
\s  \S              whitespace / non-whitespace
\\  \.  \*  etc.    literal special characters

Functions: match(pattern, string) -> Bool find(pattern, string) -> Option(String) find_all(pattern, string) -> List(String) replace(pattern, replacement, string) -> String split(pattern, string) -> List(String)

Types

ptypeRegexAtomRegexAtom =#
ptypeRegexQuantRegexQuant = QOne | QZeroOrMore | QOneOrMore | QOptional#
ptypeRegexPatternRegexPattern = RegexPattern(Bool, List(RegexItem), Bool)#
typeRegexOptsRegexOpts = { case_insensitive : Bool, multiline : Bool }#

Functions

fndefault_optsdefault_opts() : RegexOpts#

Default options: all flags disabled (case-sensitive, single-line).

fnfindfind(pattern, string)#

Find the first match of pattern in string. Returns Some(matched_substring) or None.

    Regex.find("\\d+", "price: 42 dollars")  -- Some("42")
    Regex.find("xyz", "hello")               -- None
fnfind_allfind_all(pattern, string)#

Find all non-overlapping matches of pattern in string. Returns a list of matched substrings in order.

    Regex.find_all("\\d+", "a1 b22 c333")  -- ["1", "22", "333"]
fnmatchesmatches(pattern, string)#

Test whether pattern matches anywhere in string. If pattern starts with '^', the match is anchored to the start. If pattern ends with '$', the match is anchored to the end.

    Regex.matches("\\d+", "hello 42 world")  -- true
    Regex.matches("^hello", "hello world")   -- true
    Regex.matches("^hello", "say hello")     -- false
fnmatches_optsmatches_opts(pattern, string, opts)#

Like matches, but accepts a RegexOpts record for per-call flags.

    Regex.matches_opts("HELLO", "hello world",
                       { case_insensitive: true, multiline: false })
    -- true
fnreplacereplace(pattern, replacement, string)#

Replace the first match of pattern in string with replacement. Returns the resulting string.

    Regex.replace("\\d+", "NUM", "price 42")  -- "price NUM"
fnreplace_allreplace_all(pattern, replacement, string)#

Replace all non-overlapping matches of pattern in string with replacement. Returns the resulting string.

    Regex.replace_all("\\d", "X", "a1b2c3")  -- "aXbXcX"
fnsplitsplit(pattern, string)#

Split string on matches of pattern. Returns the list of substrings between matches.

    Regex.split("\\s+", "hello   world  foo")  -- ["hello", "world", "foo"]
    Regex.split(",", "a,b,c")                   -- ["a", "b", "c"]