Skip to content
Docs
Modules
regexp

regexp

Module regexp provides regular expression matching.

The supported regular expression syntax is exactly as described in the Go regexp (opens in a new tab) documentation.

Functions

compile

Function signature
compile(expr string) regexp

Compiles a regular expression string into a regexp object.

Example
>>> regexp.compile("a+")
regexp("a+")
>>> r := regexp.compile("a+"); r.match("a")
true
>>> r := regexp.compile("[0-9]+"); r.match("nope")
false

match

Function signature
match(expr, s string) bool

Returns true if the string s contains any match of the regular expression pattern.

Example
>>> regexp.match("ab+a", "abba")
true
>>> regexp.match("[0-9]+", "nope")
false

Types

regexp

Represents a compiled regular expression.

Methods

regexp.match
Method signature
match(s string) bool

Returns true if the string s contains any match of the regular expression pattern.

Example
>>> r := regexp.compile("a+"); r.match("a")
true
regexp.find
Method signature
find(s string) string

Returns the leftmost match of the regular expression pattern in the string s.

Example
>>> r := regexp.compile("a+"); r.find("baaab")
"aaa"
regexp.find_all
Method signature
find_all(s string) []string

Returns a slice of all matches of the regular expression pattern in the string s.

Example
>>> r := regexp.compile("(du)+"); r.find_all("dunk dug in the deep end")
["du", "du"]
regexp.find_submatch
Method signature
find_submatch(s string) []string

Returns a slice of all matches of the regular expression pattern in the string s, and the matches, if any, of its subexpressions.

Example
>>> r := regexp.compile("a(b+)a"); r.find_submatch("abba")
["abba", "bb"]
regexp.replace_all
Method signature
replace_all(s, repl string) string

Returns a copy of the string s with all matches of the regular expression pattern replaced by repl.

Example
>>> r := regexp.compile("a+"); r.replace_all("baaab", "x")
"bxb"
regexp.split
Method signature
split(s string) []string

Splits the string s into a slice of substrings separated by the regular expression pattern.

Example
>>> r := regexp.compile("a+"); r.split("baaab")
["b", "b"]