Skip to content

Regex expressions¤

Overview¤

Tip

Use Regexr to develop and test regular expressions.


!REGEX¤

Regular expression search.

Type: Mapping.

Synopsis:

!REGEX
what: <string>
regex: <regex>
hit: <hit>
miss: <miss>

Scan through what string looking for any location where regular expression regex produces a match. If there is a match, then returns hit, otherwise miss is returned.

The expression hit is optional, default value is true.

The expression miss is optional, default value is false.

Example

```yaml !IF test: !REGEX what: "Hello world!" regex: "world" then: "Yes :-)" else: "No ;-("

```

Another form:

!REGEX
what: "Hello world!"
regex: "world"
hit: "Yes :-)"
miss: "No ;-("

!REGEX.REPLACE¤

Regular expression replace.

Type: Mapping.

Synopsis:

!REGEX.REPLACE
what: <string>
regex: <regex>
by: <string>

Replace regular expression regex matches in what by value of by.

Example

!REGEX.REPLACE
what: "Hello world!"
regex: "world"
by: "Mars"

Returns: Hello Mars!


!REGEX.SPLIT¤

Split a string by a regular expression.

Type: Mapping.

Synopsis:

!REGEX.SPLIT
what: <string>
regex: <regex>
max: <integer>

Split string what by regular expression regex.

An optional argument max specify the maximum number of splits.

Example

!REGEX.SPLIT
what: "07/14/2007 12:34:56"
regex: "[/ :]"

Returns: ['07', '14', '2007', '12', '34', '56']


!REGEX.FINDALL¤

Find all occurrences by a regular expression.

Type: Mapping.

Synopsis:

!REGEX.FINDALL
what: <string>
regex: <regex>

Find all matches of regex in the string what.

Example

!REGEX.FINDALL
what: "Frodo, Sam, Gandalf, Legolas, Gimli, Aragorn, Boromir, Merry, Pippin"
regex: \w+

Returns: ['Frodo', 'Sam', 'Gandalf', 'Legolas', 'Gimli', 'Aragorn', 'Boromir', 'Merry', 'Pippin']


!REGEX.PARSE¤

Parse by a regular expression.

Type: Mapping.

Synopsis:

!REGEX.PARSE
what: <string>
with: <regex>
into: <expression>

Try each regular expression from with (a single regex or a list of regexes) against what. On the first match, evaluate into.

Matched groups are available inside into as !ARG group. Group 0 is the entire match, group 1 is the first capture group, and so on.

Returns None (error) when no regex matches.

Example

!REGEX.PARSE
what: !ARG event
with: '<(\d{1,3})>(\d{1,2}) (\S+)'
into: !RECORD
  with:
    PRI: !CAST
      what: !GET {from: !ARG group, what: 1}
      type: ui16
    VERSION: !CAST
      what: !GET {from: !ARG group, what: 2}
      type: ui8
    HOSTNAME: !GET {from: !ARG group, what: 3}

For parsing characters with parser combinators, see !PARSE.REGEX.


!REGEX.EXPAND¤

Expand shorthand strings into a list of alternatives.

Type: Mapping.

Synopsis:

!REGEX.EXPAND
what:
  - <string>
  - <string>

Supported expansions:

  • "ReadData (or ListDirectory)" → ["ReadData", "ListDirectory"]
  • "Execute/Traverse" → ["Execute", "Traverse"]

Example

!REGEX.EXPAND
what:
  - "ReadData (or ListDirectory)"