re
Regular expressions: search text with patterns like "\d+" (digits) or "\w+@\w+". Uses Python's pattern syntax.
import re
Example
import re
let line = "Failed password for root from 203.0.113.42 port 22"
say(re.test("Failed password", line))
let m = re.match("for (?P<user>\\w+) from (?P<ip>[\\d.]+)", line)
say(m.user, m.ip)
say(re.find_all("\\d+", line))
say(re.replace("\\d+\\.\\d+\\.\\d+\\.\\d+", line, "<ip>"))
Functions
test(pattern, text)
returns boolTrue if the pattern appears anywhere in the text.
match(pattern, text)
returns map or nilThe first place the pattern matches: m[0] is the whole match, m[1], m[2]... the groups in (), named groups (?P<name>...) by name, plus "start" and "end". nil if there is no match.
find_all(pattern, text)
returns listEvery match. With one group in () you get that group's text; with several, a list of groups for each match.
replace(pattern, text, with)
returns textSwap every match for with. In with, \1 means group 1. with can also be a function that gets the match map and gives back text.
split(pattern, text)
returns listCut the text at every match: split(",\s*", "a, b,c") is ["a", "b", "c"].
escape(text)
returns textThe text with pattern characters made safe, to search for it exactly.