regex_search¶
Collection Note
This module is part of the ansible.builtin collection. To install the collection, use:
Added in version2.0.
Synopsis¶
- Search in a string to extract the part that matches the regular expression.
Parameters¶
| Parameter | Defaults / Choices | Comments |
|---|---|---|
| _input str required |
String to match against. | |
| _regex str |
Regular expression string that defines the match. | |
| ignorecase bool |
Force the search to be case insensitive if V(True), case sensitive otherwise. | |
| multiline bool |
Search across line endings if V(True), do not if otherwise. |
Notes¶
Note
- Maps to Python's C(re.search).
- The substring matched by the group is accessible via the symbolic group name or the
\{number}special sequence. See examples section.
Examples¶
# db => 'database42'
db: "{{ 'server1/database42' | regex_search('database[0-9]+') }}"
# Using inline regex flags instead of passing options to filter
# See https://docs.python.org/3/library/re.html for more information
# on inline regex flags
# server => 'sErver1'
db: "{{ 'sErver1/database42' | regex_search('(?i)server([0-9]+)') }}"
# drinkat => 'BAR'
drinkat: "{{ 'foo\nBAR' | regex_search('^bar', multiline=True, ignorecase=True) }}"
# Extracts server and database id from a string using number
# (the substring matched by the group is accessible via the \number special sequence)
db: "{{ 'server1/database42' | regex_search('server([0-9]+)/database([0-9]+)', '\\1', '\\2') }}"
# => ['1', '42']
# Extracts dividend and divisor from a division
# (the substring matched by the group is accessible via the symbolic group name)
db: "{{ '21/42' | regex_search('(?P<dividend>[0-9]+)/(?P<divisor>[0-9]+)', '\\g<dividend>', '\\g<divisor>') }}"
# => ['21', '42']
Return Values¶
| Key | Data Type | Description | Returned |
|---|---|---|---|
| _value | str | Matched string or C(None) if no match. |