text
change case, split and join, search and replace in text, with regular expressions, and read numbers written in words
Working with strings: changing case, splitting and joining, and searching and replacing.
writing
Section titled “writing”A regular expression (regex) is a pattern for matching text, written r"...": r"\d+" matches a run of digits. Functions that search accept either a plain string, matched exactly, or a regex. Parentheses in a regex capture the part they match. match returns the captured parts, and in replace, $1 stands for the first one.
upperthe string in UPPERCASElowerthe string in lowercasetrimremove spaces, tabs and newlines from both endscapitalizemake the first character uppercasesplitsplit into a list of pieces wherever sep appears (a string or a regex)from_wordsan English number in words, like “three hundred twelve”, as an integerlinessplit into a list of linescharssplit into a list of single charactersjoinjoin a list’s items into one string, with sep between themreplacereplace every match of pat with the given textstarts_withwhether s starts with prefixends_withwhether s ends with suffixmatchthe first match of a regex, or nil if there’s nonefind_alla list of every match of patgrepthe lines of a string, or items of a list, that contain patrepeatthe string repeated n timesnumsevery number found in a string, as a list of numbersstrip_ansiremove the hidden codes that color terminal text or move the cursor, leaving the plain text
1# str: text in double quotes2"héllo\tworld"3# regex: a pattern, r and then quotes4r"\d+"syntax
Section titled “syntax”1# str + anything joins them as text2"v" + 2"v2"3# str * n repeats it n times4"ab" * 3"ababab"Functions
Section titled “Functions”upper(s: str) -> str
the string in UPPERCASE
1"hello".upper"HELLO"2upper("zil")"ZIL"Tags: formatting
See also lower, capitalize
lower(s: str) -> str
the string in lowercase
1"HeLLo".lower"hello"Tags: formatting
See also upper, capitalize
trim(s: str) -> str
remove spaces, tabs and newlines from both ends
Also called strip.
1" hi ".trim"hi"Tags: formatting
See also split
capitalize
Section titled “capitalize”capitalize(s: str) -> str
make the first character uppercase
1"hello world".capitalize"Hello world"Tags: formatting
See also upper
split(s: str, sep?: str|regex) -> list
split into a list of pieces wherever sep appears (a string or a regex)
Without sep, split on runs of whitespace
1"a b c".split["a", "b", "c"]2"x,y;z".split(r"[,;]")["x", "y", "z"]Tags: parsing
from_words
Section titled “from_words”from_words(s: str) -> int
an English number in words, like “three hundred twelve”, as an integer
Reads minus, hyphens, an optional “and” and the scale words up to quintillion, short scale unless number_scale is “long”
1from_words("three hundred twelve")3122from_words("minus two million, forty-five")-2000045Tags: parsing
See also spell
lines(s: str) -> list
split into a list of lines
1"a\nb".lines["a", "b"]Tags: parsing
See also split
chars(s: str) -> list
split into a list of single characters
1"abc".chars["a", "b", "c"]Tags: parsing
See also split
join(xs: list, sep: str = “”) -> str
join a list’s items into one string, with sep between them
1["a", "b"].join("-")"a-b"2[1, 2].join"12"Tags: formatting
See also split
replace
Section titled “replace”replace(s: str, pat: str|regex, with: str) -> str
replace every match of pat with the given text
With a regex, $1 in it stands for what the first capture group matched, $2 the second, and so on
1"a.b".replace(".", "-")"a-b"2"a b c".replace(r"\s+", " ")"a b c"Tags: formatting
starts_with
Section titled “starts_with”starts_with(s: str, prefix: str) -> bool
whether s starts with prefix
1"abc".starts_with("a")trueTags: searching
ends_with
Section titled “ends_with”ends_with(s: str, suffix: str) -> bool
whether s ends with suffix
1"file.zil".ends_with(".zil")trueTags: searching
See also starts_with
match(s: str, re: regex) -> str|list|nil
the first match of a regex, or nil if there’s none
If the regex has capture groups (parentheses), a list of what each captured instead
1"2026-10-06".match(r"(\d+)-(\d+)")["2026", "10"]2"id 42".match(r"\d+")"42"find_all
Section titled “find_all”find_all(s: str, pat: str|regex) -> list
a list of every match of pat
If the regex has capture groups (parentheses), each match becomes a list of what each group captured instead
1"a1b22c333".find_all(r"\d+")["1", "22", "333"]2"a=1, b=2".find_all(r"(\w)=(\d)")[["a", "1"], ["b", "2"]]Tags: searching
grep(v: str|list, pat: str|regex, *, ignore_case: bool = false, invert: bool = false, count: bool = false) -> list|int
the lines of a string, or items of a list, that contain pat
1"ok\nERROR 1\nERROR 2".grep("ERROR")["ERROR 1", "ERROR 2"]2["a1", "b", "c22"].grep(r"\d")["a1", "c22"]As a command:
-i,--ignore-caseignore case-v,--invertkeep the lines that don’t match instead-c,--countcount the matches instead
grep -i err app.log# → grep(read_file("app.log"), "err", ignore_case: true)See also lines, filter, contains
repeat
Section titled “repeat”repeat(s: str, n: int) -> str
the string repeated n times
1"ab".repeat(3)"ababab"Tags: formatting
nums(s: str) -> list
every number found in a string, as a list of numbers
1"x=3, y=-2.5; 1e3".nums[3, -2.5, 1000]Tags: parsing
strip_ansi
Section titled “strip_ansi”strip_ansi(s: str) -> str
remove the hidden codes that color terminal text or move the cursor, leaving the plain text
1"{chr(27)}[1mbold{chr(27)}[0m".strip_ansi"bold"Tags: formatting
More examples
Section titled “More examples”1# numbers out of text2"a1b22c333".nums.sum3563# the parts a regex captures4"2026-10-06".match(r"(\d+)-(\d+)-(\d+)")["2026", "10", "06"]5# a space before each capital, $1 being the capture6"CamelCaseName".replace(r"(\B[A-Z])", " $1")"Camel Case Name"7# only the lines that match8"ok\nERROR disk full\nok".grep(r"ERROR")["ERROR disk full"]9# initials10"Ada Lovelace".split.map(|w| w[0]).join"AL"11# printf-style formatting12format("%-6s|%5.2f", "pi", pi)"pi | 3.14"Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.