Skip to content

PPA Tools PPA Tools: Regex

Summary

This module contains actions for searching & modifying text using regular expressions.

Actions

ppa_tools.regex.

replace

Replace a string with another inside some text using a regular expression.

Minimum Plugin Version: 5.6.0

Input
  • text: the text to process

  • expression: a regular expression that describes the text to replace

  • replacement: a string to replace any expression matches with

No Matches

This action will succeed and return the original text if no expression matches are found.

Output

A new copy of text with any replacements made.

Example

Removing all lines that end with - England:

- ppa_tools.regex.replace:
    expression: >
      .*\-\sEngland\n
    replacement: ""
    text: >
      London - England
      Manchester - England
      Paris - France
      New York - USA
  save: replaced

The output will be:

Paris - France
New York - USA

ppa_tools.regex.

Search for values in text using either:

  • a simple regular expression

  • multiple expressions with named matches

See below for an example of each approach.

Minimum Plugin Version: 10.3.0

Regular Expressions

The following regex cheat-sheet & visualiser can help you get started with expressions.

Input
  • text: the text to find & extract values from

  • expression: a single regular expression (introduced in 10.3.0)

  • matches: a single or list of Matches

Expression & Matches

Only 1 of these inputs may be supplied.

Expression:

  • Treated as a multiline expression
  • Evaluated against text once, not per-line
  • Can contain up to 1 capturing group (optional)
  • Cannot match more than one value in text

Matches:

  • Can be treated as single or multiline (see Match for more information)
  • Can contain up to 1 capturing group (optional)
  • Cannot match more than one value per-line
Output

If expression was supplied:

  • the matched value or null if no match was found

If matches was supplied:

  • A dictionary containing the name & matched value of each supplied expression

  • If no match was found for an expression, its matched value will be null

Using Expression Input

Finding the TACACS group a user is in.

1
2
3
4
5
6
7
8
9
- ppa_tools.regex.search:
    expression: >
      user\s=\sp_example.user.*\n.*\n\s+member\s=\s(.*)\n\s*\}
    text: >
      user = p_example.user {
          login = internal
          member = engineering
      }
  save: match

The contents of the new match variable will be:

1
engineering
Using Matches Input

Finding the TACACS group a user is in.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
- ppa_tools.regex.search:
    matches:
      name: tacacs_group
      multiline: true
      expression: >
        user\s=\sp_example.user.*\n.*\n\s+member\s=\s(.*)\n\s*\}
    text: >
      user = p_example.user {
          login = internal
          member = engineering
      }
  save: matches

The contents of the new matches variable will be:

1
tacacs_group: engineering