Skip to content

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.

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.

  • upper the string in UPPERCASE
  • lower the string in lowercase
  • trim remove spaces, tabs and newlines from both ends
  • capitalize make the first character uppercase
  • split split into a list of pieces wherever sep appears (a string or a regex)
  • from_words an English number in words, like “three hundred twelve”, as an integer
  • lines split into a list of lines
  • chars split into a list of single characters
  • join join a list’s items into one string, with sep between them
  • replace replace every match of pat with the given text
  • starts_with whether s starts with prefix
  • ends_with whether s ends with suffix
  • match the first match of a regex, or nil if there’s none
  • find_all a list of every match of pat
  • grep the lines of a string, or items of a list, that contain pat
  • repeat the string repeated n times
  • nums every number found in a string, as a list of numbers
  • strip_ansi remove the hidden codes that color terminal text or move the cursor, leaving the plain text
1
# str: text in double quotes
2
"héllo\tworld"
3
# regex: a pattern, r and then quotes
4
r"\d+"
Example outputCtrl+Enter to run
1
# str + anything joins them as text
2
"v" + 2"v2"
3
# str * n repeats it n times
4
"ab" * 3"ababab"
Example outputCtrl+Enter to run
upper(s: str) -> str

the string in UPPERCASE

1
"hello".upper"HELLO"
2
upper("zil")"ZIL"
Example outputCtrl+Enter to run

Tags: formatting

See also lower, capitalize

lower(s: str) -> str

the string in lowercase

1
"HeLLo".lower"hello"
Example outputCtrl+Enter to run

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"
Example outputCtrl+Enter to run

Tags: formatting

See also split

capitalize(s: str) -> str

make the first character uppercase

1
"hello world".capitalize"Hello world"
Example outputCtrl+Enter to run

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"]
Example outputCtrl+Enter to run

Tags: parsing

See also join, lines, chars

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”

1
from_words("three hundred twelve")312
2
from_words("minus two million, forty-five")-2000045
Example outputCtrl+Enter to run

Tags: parsing

See also spell

lines(s: str) -> list

split into a list of lines

1
"a\nb".lines["a", "b"]
Example outputCtrl+Enter to run

Tags: parsing

See also split

chars(s: str) -> list

split into a list of single characters

1
"abc".chars["a", "b", "c"]
Example outputCtrl+Enter to run

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"
Example outputCtrl+Enter to run

Tags: formatting

See also split

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"
Example outputCtrl+Enter to run

Tags: formatting

See also find_all, match

starts_with(s: str, prefix: str) -> bool

whether s starts with prefix

1
"abc".starts_with("a")true
Example outputCtrl+Enter to run

Tags: searching

See also ends_with, contains

ends_with(s: str, suffix: str) -> bool

whether s ends with suffix

1
"file.zil".ends_with(".zil")true
Example outputCtrl+Enter to run

Tags: 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"
Example outputCtrl+Enter to run

See also find_all, replace

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"]]
Example outputCtrl+Enter to run

Tags: searching

See also match, count

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"]
Example outputCtrl+Enter to run

As a command:

  • -i, --ignore-case ignore case
  • -v, --invert keep the lines that don’t match instead
  • -c, --count count the matches instead
grep -i err app.log
# → grep(read_file("app.log"), "err", ignore_case: true)

See also lines, filter, contains

repeat(s: str, n: int) -> str

the string repeated n times

1
"ab".repeat(3)"ababab"
Example outputCtrl+Enter to run

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]
Example outputCtrl+Enter to run

Tags: parsing

See also find_all, parse

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"
Example outputCtrl+Enter to run

Tags: formatting

See also rainbow, len

1
# numbers out of text
2
"a1b22c333".nums.sum356
3
# the parts a regex captures
4
"2026-10-06".match(r"(\d+)-(\d+)-(\d+)")["2026", "10", "06"]
5
# a space before each capital, $1 being the capture
6
"CamelCaseName".replace(r"(\B[A-Z])", " $1")"Camel Case Name"
7
# only the lines that match
8
"ok\nERROR disk full\nok".grep(r"ERROR")["ERROR disk full"]
9
# initials
10
"Ada Lovelace".split.map(|w| w[0]).join"AL"
11
# printf-style formatting
12
format("%-6s|%5.2f", "pi", pi)"pi | 3.14"
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.