context_from_regex¶
Operation to extract context data from a file using a regex.
Example¶
The following example uses a regex to extract the project name from a python project’s
pyproject.toml
:
steps:
"Get project name from pyproject.toml":
name: context_from_regex
file_path: "pyproject.toml"
regex: '^name = "(?P<project_name>[^"]+)"'
opconfig:
output_namespace: template_variables
"Display result":
name: echo
message: |
Detected project name: {{ qwikstart.project_name }}
Note that the regex must be defined as a named capture group, where the name of the
capture group (the project_name
part of (?P<project_name>[^"]+)
in this case)
specifies the name where the value is stored.
In order to render the context variable using the echo
operation, the project_name
is saved to the template_variables
namespace by defining
output_namespace = "template_variables"
. See Understanding operations and
the docs for the echo operation for more info.
Required context¶
regex
Regex to search for in
file_path
. Note that this is expected to contain named capture groups. Names of capture groups define new context variable names.See https://docs.python.org/3/howto/regex.html#non-capturing-and-named-groups
file_path
Path to file relative to the current working directory.
Optional context¶
regex_flags
default:
["MULTILINE"]
List of Python regex flags. Any combination of
'IGNORECASE'
,'MULTILINE'
,'DOTALL'
,'UNICODE'
. See docs for Python regex library.
Output¶
This operation can define arbitrary output values based on named capture groups in
regex
.