elvish-re(7)

πŸ“¦
version 0.17.0 Β· 2 versions
, in ubuntu jammy

🌸 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:


✨ 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 $name or ${name} patterns to refer to capture groups, where name consists of letters, digits and underscores. purely numeric patterns like $1 refer 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