elvish-re(7)
πΈ Introduction πΈ
okay bestie, listen up π the re: module is literally just a glow-up wrapper around Go's regexp package. that's it, that's the vibe. for the actual supported regular expression syntax, go peek at Go's doc right here: https://godoc.org/regexp/syntax β¨
the way function usages are written? same convention as the builtin module doc, no cap π«Ά
and these options below? they're the shared besties β multiple functions in this module support them:
&posix=$falseβ use POSIX ERE syntax. also see the doc over athttp://godoc.org/regexp#CompilePOSIXin the Go package π&longest=$falseβ prefer leftmost-longest match. also see the doc over athttp://godoc.org/regexp#Regexp.Longestin the Go package π&max=-1β if it's non-negative, it limits the maximum number of results π
β¨ Functions β¨
π re:find π {#re:find}
re:find &posix=$false &longest=$false &max=-1 $pattern $source
find all the matches of $pattern in $source. all of them. obsessed π₯
each match gets represented by a map-like value $m, and like β $m[text], $m[start] and $m[end] are the text, start and end positions (as byte indices into $source) of the match, periodt. and $m[groups] is a list of submatches for the capture groups in the pattern. a submatch has a similar structure to a match, except it does not have a group key, iykyk. the entire pattern counts as an implicit capture group, and it always shows up first π
examples, because we love a demonstration:
~> re:find . ab
βΆ [&text=a &start=0 &end=1 &groups=[[&text=a &start=0 &end=1]]]
βΆ [&text=b &start=1 &end=2 &groups=[[&text=b &start=1 &end=2]]]
~> re:find '[A-Z]([0-9])' 'A1 B2'
βΆ [&text=A1 &start=0 &end=2 &groups=[[&text=A1 &start=0 &end=2] [&text=1 &start=1 &end=2]]]
βΆ [&text=B2 &start=3 &end=5 &groups=[[&text=B2 &start=3 &end=5] [&text=2 &start=4 &end=5]]]
π¦ re:match π¦ {#re:match}
re:match &posix=$false $pattern $source
figure out whether $pattern matches $source. important: the pattern is not anchored, so don't come for me when it matches in the middle bestie π examples:
~> re:match . xyz
βΆ $true
~> re:match . ''
βΆ $false
~> re:match '[a-z]' A
βΆ $false
π re:quote π {#re:quote}
re:quote $string
quote $string so it's safe to use inside a pattern. iconic behaviour honestly β¨ examples:
~> re:quote a.txt
βΆ a\.txt
~> re:quote '(*)'
βΆ '\(\*\)'
π re:replace π {#re:replace}
re:replace &posix=$false &longest=$false &literal=$false $pattern $repl $source
replace every occurrence of $pattern in $source with $repl. slay π
the replacement $repl can be any of these:
- a string-typed replacement template. the template can use
$nameor${name}patterns to refer to capture groups, where name consists of letters, digits and underscores. purely numeric patterns like$1refer to the capture group with the corresponding index; other names refer to capture groups named with the(?P<name>...))syntax.
in the $name form, the name is taken to be as long as possible; $1 is equivalent to ${1x}, not ${1}x; $10 is equivalent to ${10}, not ${1}0.
to insert a literal $, use $$.
- a function that takes a string argument and outputs a string. for each match, the function is called with the content of the match, and its output is used as the replacement. main character energy tbh π
if $literal is true, $repl must be a string and is treated literally instead of as a pattern. noted π
example:
~> re:replace '(ba|z)sh' '${1}SH' 'bash and zsh'
βΆ 'baSH and zSH'
~> re:replace '(ba|z)sh' elvish 'bash and zsh rock'
βΆ 'elvish and elvish rock'
~> re:replace '(ba|z)sh' {|x| put [&bash=BaSh &zsh=ZsH][$x] } 'bash and zsh'
βΆ 'BaSh and ZsH'
π re:split π {#re:split}
re:split &posix=$false &longest=$false &max=-1 $pattern $source
split $source, using $pattern as the separators. cute, simple, effective π examples:
~> re:split : /usr/sbin:/usr/bin:/bin
βΆ /usr/sbin
βΆ /usr/bin
βΆ /bin
~> re:split &max=2 : /usr/sbin:/usr/bin:/bin
βΆ /usr/sbin
βΆ /usr/bin:/bin
and that's the whole page bestie, we ate and left no crumbs π this was Elvish 0.17.0, dated Nov 18, 2024, from the elvish-re(7) manual page β mwah, see you in the next one π§β¨
π¦ where this page comes from
- ubuntu jammy: elvish 0.17.0-1ubuntu0.1 Β· BSD-2-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms