A regular expression, commonly known as regex, describes a pattern of characters.
Usage: They are often used to perform searches, replace substrings and validate string data.
Alternation has the pipe symbol | . It allows us to match either the characters preceding the | or after the |
For example: cat|dog will match cat as well as dog.
Character sets denoted by a pair of brackets [] will match any of the characters included within the brackets.
Wildcards denoted with the period can match any single character (letter, number, symbol or whitespace)
For example:
… will match cat or dog or any other 3-character text.
To match an actual period . , youcan use the escape character .
Regular expression ranges are used to specify a range of characters that can be matched.
Common regular expression ranges include:
Shorthand character classes simplify writing regular expressions.
Grouping lets us group parts of a regular expression together.
For example: I like (tea|coffee) will match the text I like and then match either tea or coffee.
Fixed quantifiers are indicated by curly braces {}
Optional quantifiers with question mark ? allows us to indicate a character in a regex is optional.
Kleene star * indicates that the preceding character can occur 0 or more times.
For example: ha*t will match hat, haat, haaaat, or haaaaaaaaat
Kleene plus + indicates that the preceding character can occur 1 or more times.
For example: meo+w will match meow, meooow, and meoooooooooooow, but not match mew.
Anchors (hat ^ and dollar sign $) are used in regular expressions to match text at the start and end of a string.