Every programmer should learn to use these, but approach them with the respect of learning another programming language, because that’s really what they are. Get a good book.
There are two popular regex styles:
PCREs in some form are used natively in some languages, e.g.:
Beware that even PCREs amongst languages have slight feature differences and quirks:
.
actually matchesPCRE basics:
.
any character^
beginning of line$
end of line[ ]
positive set[^ ]
negative set-
in a set to denote range between characters\d
equivalent to [0-9]
\w
equivalent to [a-zA-Z0-9_]
\s
equivalent to [ \t\r\n\f]
()
capturing group(?:)
non-capturing group(?P<var>)
named capturing group (named var)|
{M}
exactly M times{M,}
mininum M times{M,N}
minimum M maximum N times*
equivalent to {0,}
+
equivalent to {1,}
?
equivalent to {0,1}
x(?=y)
match x only if it is followed by yx(?!y)
match x only if it is not followed by y(?<=y)x
match x only if it follows y(?<!y)x
match x only if it does not follow yBash globbing (filename expansions) look similar to regex but are not. They are still very useful:
?
single character wildcard*
multi character wildcard[ ]
bracket expression[^ ]
negated bracket expression[-]
options range
^
negate range (place inside bracket)Globbing. Regular Expressions. Advanced Bash-Scripting Guide.
Filenames and Pathnames in Shell: How to do it Correctly. David A. Wheeler. 2016-05-04.
Perl Style Regular Expressions in Prolog. Robert D. Cameron. CMPT 384 Lecture Notes. 1999.
Regular Expression. Wikipedia.
RegExp. MDN.
RegExp lookbehind assertions. Yang Guo. V8 JavaScript Engine. 2016-02-26.