Regexes
A regex (regular expression) is a way to find and replace using a pattern.
The syntax can look confusing at first, but it is very powerful and flexible.
Regex strings are prefixed with a lowercase 'r'. (e.g. r'abc'
).
NoteBackslashes
\
are key part of regex syntax, so THT treats them as literals. They do not need to be escaped unless you want to match a literal backslash character (e.g. \\
).$pattern = r'(orange|red|yellow)' 'large red hoodie'.contains($pattern) //= true // Match one or more digits 'The number is: 123'.match(r'\d+') //= '123' // Match one or more word characters (a-z) 'abc 123'.match(r'\w+') //= 'abc' // Match one or more characters of // lowercase 'a' through 'f' 'ID: 123-456-edca'.match(r'[a-f]+') //= 'edca' // Ignore case ('i' suffix) 'Hi, I`m Bea Goode'.match(r'[a-f]+'i) //= 'Bea' // Create a Regex from a regular string $pattern = Regex('\\d+ \\w+', 'i')