Regular Expressions
Regular
expressions are a minilanguage used to describe patterns of strings.
A regular expression literal is a pattern between slashes or between
arbitrary delimiters followed by %r:
/pattern/ /pattern/im # option can be specified %r!/usr/local! # general delimited regular expression
Regular expressions have their own power and mystery; for more on this topic, see O’Reilly’s Mastering Regular Expressions by Jeffrey E.F. Friedl.
Regular-expression modifiers
Regular expression literals may include an optional modifier to control various aspects of matching. The modifier is specified after the second slash character, as shown previously and may be represented by one of these characters:
- i
Case-insensitive
- o
Substitutes only once
- x
Ignores whitespace and allows comments in regular expressions
- m
Matches multiple lines, recognizing newlines as normal characters
Regular-expression patterns
Except for control characters,
(+ ? .
* ^ $
( ) [
] { }
| \), all characters match
themselves. You can escape a control character by preceding it with a
backslash.
Regular characters that express
repetition (* +
{ }) can match very long
strings, but when you follow such characters with control characters
?, you invoke a nongreedy match that finishes at
the first successful match (i.e., +,
*, etc.) followed by ? (i.e.,
+?, *?, etc.).
^Matches beginning of line.
$Matches end of line.
.Matches any single character except newline. Using
moption allows it to match newline as well.[...]Matches any ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access