regex
Searching and replacing with a pattern: finding a number, an email address or a date in text and pulling it apart.
import regex
A regular expression is a small language for describing the shape of text; [0-9]+ means "one or more digits". With one you can ask whether text matches a pattern, find every place it matches, and pull pieces out or replace them.
Every function comes in two forms. The one that takes a Regex uses a pattern compiled in advance and is faster when you use it repeatedly. The one whose name ends in Str takes the pattern as text and compiles it each time; that is more convenient for a one-off.
Parentheses in a pattern make a "group", and (?P<name>...) makes a named one. Group zero is the whole match. In a replacement, $1 and $2 stand for the groups.
Types
MatchResult
struct MatchResult| Name | Type | Description |
|---|---|---|
ok | bool = false | True when a match was found. |
start | int = -1 | Where the match starts, in bytes. |
stop | int = -1 | Where the match ends, in bytes; that position is not part of it. |
count | int = 0 | The number of groups, not counting group zero. |
text | str = "" | The whole text that was searched, not only the part that matched. For the match itself use Group(m, 0). |
pattern | str = "" | The pattern the search was made with. |
gs | int[] | Where each group starts. |
ge | int[] | Where each group ends. |
import regex
func main:
r := regex.Compile("(?P<year>[0-9]{4})-(?P<month>[0-9]{2})")
m := regex.FindMatch(r, "date: 2025-09")
println m.ok, m.start, m.stop, m.text
println regex.GroupCount(m), regex.GroupOk(m, 1)
println regex.Group(m, 0), regex.Group(m, 1), regex.Group(m, 2)
println regex.GroupStart(m, 2), regex.GroupEnd(m, 2)
println regex.GroupIndex(m, "month"), regex.GroupByName(m, "year")
m2 := regex.FindMatchFrom(r, "2024-01 and 2025-02", 5)
println regex.Group(m2, 0)
println regex.Group(regex.FindMatchStr("b+", "aabbbc"), 0)
println regex.FindSubmatch(r, "2025-09")
println regex.FindSubmatchStr("(a)(b)", "xab")
endRegex
struct RegexCompile and use it many times.| Name | Type | Description |
|---|---|---|
pattern | str = "" | The text of the pattern that was compiled. |
import regex
func main:
r := regex.Compile("[0-9]+")
println r.pattern, regex.IsValid("[0-9]+"), regex.IsValid("[0-9")
println regex.Match(r, "abc123"), regex.Find(r, "abc123")
println regex.Replace(r, "a1 b22 c333", "#")
println regex.ReplaceAll(r, "a1 b22 c333", "#")
println regex.MatchStr("^[a-z]+$", "salam"), regex.FindStr("o", "salam donya")
println regex.ReplaceStr("a", "banana", "o"), regex.ReplaceAllStr("a", "banana", "o")
endFunctions
FindMatch
func FindMatch(re: Regex, text: str): MatchResultThe example for this item is shown with the ones above: regex-Group.salam
FindMatchStr
func FindMatchStr(pattern: str, text: str): MatchResultFindMatch, with the pattern as text.The example for this item is shown with the ones above: regex-Group.salam
FindMatchFrom
func FindMatchFrom(re: Regex, text: str, from: int): MatchResultFindMatch, but starts the search at byte from, for finding the next occurrence.The example for this item is shown with the ones above: regex-Group.salam
GroupCount
func GroupCount(m: MatchResult): intThe example for this item is shown with the ones above: regex-Group.salam
GroupOk
func GroupOk(m: MatchResult, i: int): booli took part in this match. An optional group may stay empty.The example for this item is shown with the ones above: regex-Group.salam
Group
func Group(m: MatchResult, i: int): stri; group zero is the whole match.The example for this item is shown with the ones above: regex-Group.salam
GroupStart
func GroupStart(m: MatchResult, i: int): intThe example for this item is shown with the ones above: regex-Group.salam
GroupEnd
func GroupEnd(m: MatchResult, i: int): intThe example for this item is shown with the ones above: regex-Group.salam
GroupIndex
func GroupIndex(m: MatchResult, name: str): intThe example for this item is shown with the ones above: regex-Group.salam
GroupByName
func GroupByName(m: MatchResult, name: str): strThe example for this item is shown with the ones above: regex-Group.salam
FindSubmatch
func FindSubmatch(re: Regex, text: str): Vector<str>The example for this item is shown with the ones above: regex-Group.salam
FindSubmatchStr
func FindSubmatchStr(pattern: str, text: str): Vector<str>FindSubmatch, with the pattern as text.The example for this item is shown with the ones above: regex-Group.salam
FindAllMatches
func FindAllMatches(re: Regex, text: str, limit: int): Vector<MatchResult>limit of minus one means no limit.import regex
func main:
r := regex.Compile("[0-9]+")
text := "year 1403, month 7, day 12"
println regex.FindAll(r, text)
println regex.FindAllN(r, text, 2)
println regex.FindAllStr("[a-z]+", "one two three")
println regex.Split(regex.Compile(" *, *"), "a , b,c , d")
println regex.SplitN(regex.Compile(","), "a,b,c,d", 2)
println regex.SplitStr("-+", "2025--09---29")
all := regex.FindAllMatches(r, text, -1)
each m in all:
println regex.Group(m, 0), m.start, m.stop
end
endFindAllN
func FindAllN(re: Regex, text: str, limit: int): Vector<str>limit matches.The example for this item is shown with the ones above: regex-FindAll.salam
FindAll
func FindAll(re: Regex, text: str): Vector<str>The example for this item is shown with the ones above: regex-FindAll.salam
FindAllStr
func FindAllStr(pattern: str, text: str): Vector<str>FindAll, with the pattern as text.The example for this item is shown with the ones above: regex-FindAll.salam
SplitN
func SplitN(re: Regex, text: str, limit: int): Vector<str>limit pieces; the last piece is the rest of the text.The example for this item is shown with the ones above: regex-FindAll.salam
Split
func Split(re: Regex, text: str): Vector<str>str.Split.The example for this item is shown with the ones above: regex-FindAll.salam
SplitStr
func SplitStr(pattern: str, text: str): Vector<str>Split, with the pattern as text.The example for this item is shown with the ones above: regex-FindAll.salam
Expand
func Expand(m: MatchResult, template: str): str$1, $2 and so on stand for the groups.import regex
func main:
r := regex.Compile("([a-z]+)@([a-z]+)")
text := "ali@home, sara@work"
m := regex.FindMatch(r, text)
println regex.Expand(m, "$2 <- $1")
println regex.ReplaceExpand(r, text, "$2:$1")
println regex.ReplaceAllExpand(r, text, "$2:$1")
println regex.ReplaceExpandStr("(\\w+) (\\w+)", "hello world", "$2 $1")
println regex.ReplaceAllExpandStr("(a)(b)", "abab", "$2$1")
endReplaceExpand
func ReplaceExpand(re: Regex, text: str, template: str): str$1 and the rest.The example for this item is shown with the ones above: regex-Expand.salam
ReplaceAllExpand
func ReplaceAllExpand(re: Regex, text: str, template: str): strThe example for this item is shown with the ones above: regex-Expand.salam
ReplaceExpandStr
func ReplaceExpandStr(pattern: str, text: str, template: str): strReplaceExpand, with the pattern as text.The example for this item is shown with the ones above: regex-Expand.salam
ReplaceAllExpandStr
func ReplaceAllExpandStr(pattern: str, text: str, template: str): strReplaceAllExpand, with the pattern as text.The example for this item is shown with the ones above: regex-Expand.salam
Compile
func Compile(pattern: str): RegexIsValid first.The example for this item is shown with the ones above: regex-Compile.salam
IsValid
func IsValid(pattern: str): boolThe example for this item is shown with the ones above: regex-Compile.salam
Match
func Match(re: Regex, text: str): bool^ and $.The example for this item is shown with the ones above: regex-Compile.salam
Find
func Find(re: Regex, text: str): intThe example for this item is shown with the ones above: regex-Compile.salam
Replace
func Replace(re: Regex, text: str, replacement: str): strThe example for this item is shown with the ones above: regex-Compile.salam
ReplaceAll
func ReplaceAll(re: Regex, text: str, replacement: str): strThe example for this item is shown with the ones above: regex-Compile.salam
MatchStr
func MatchStr(pattern: str, text: str): boolMatch, with the pattern as text.The example for this item is shown with the ones above: regex-Compile.salam
FindStr
func FindStr(pattern: str, text: str): intFind, with the pattern as text.The example for this item is shown with the ones above: regex-Compile.salam
ReplaceStr
func ReplaceStr(pattern: str, text: str, replacement: str): strReplace, with the pattern as text.The example for this item is shown with the ones above: regex-Compile.salam
ReplaceAllStr
func ReplaceAllStr(pattern: str, text: str, replacement: str): strReplaceAll, with the pattern as text.The example for this item is shown with the ones above: regex-Compile.salam