regex.findall
The regex.findall function returns every non-overlapping match of a regular expression, in the order they appear.
Use it to extract identifiers, numbers, or fields from command output and file contents.
Usage
regex.findall(pattern, text)
Arguments
patternRequired. The regular expression, as a string, in RE2 syntax. Inline flags such as
(?i)and(?m)work. Lookahead, lookbehind, and backreferences in the pattern are not available.text- Required. The string to search.
Both arguments are required and can be passed positionally or by keyword.
Returns
A list of strings, one for each complete match. Capture groups do not change the result: each element is the whole matched text, not the contents of a group. When nothing matches, the result is an empty list.
Examples
Extract numbers
output = regex.findall("[0-9]+", "a1 b22 c333")
["1","22","333"]
Capture groups return the whole match
print(regex.findall("(\\w+)@(\\w+)\\.com", "ann@acme.com bob@corp.com"))
["ann@acme.com", "bob@corp.com"]
To pick out one part of each match, run the pattern again on the matched text, or write a pattern that matches only the part you want.
Read values from a file
text = fs.read_file("app.yaml")
region = regex.findall("region: (.*)", text)
Here region is ["region: us-east-2"] for a file that contains the line region: us-east-2. Take the part after
the colon with split or a second pattern.
No matches
print(regex.findall("zzz", "abc"))
[]
Errors
- A pattern that is not valid RE2 fails with
invalid regex: ...followed by the reason. - A missing argument, or an argument that is not a string, fails with an argument error.
Related
regex.searchtests whether a pattern matches.regex.replacerewrites matched text.fs.read_filesupplies text to search.- Atmos Automation Language and the script step