In Regexes§
See primary documentation in context for Alternation: ||
To match one of several possible alternatives, separate them by ||
; the first matching alternative wins.
For example, ini
files have the following form:
[section] key = value
Hence, if you parse a single line of an ini
file, it can be either a section or a key-value pair and the regex would be (to a first approximation):
/ '[' \w+ ']' || \S+ \s* '=' \s* \S* /
That is, either a word surrounded by square brackets, or a string of non-whitespace characters, followed by zero or more spaces, followed by the equals sign =
, followed again by optional whitespace, followed by another string of non-whitespace characters.
An empty string as the first branch is ignored, to allow you to format branches consistently. You could have written the previous example as
/|| '[' \w+ ']'|| \S+ \s* '=' \s* \S*/
Even in non-backtracking contexts, the alternation operator ||
tries all the branches in order until the first one matches.