RegularExpressions
RegularExpressions
Regular Expressions
• A regular expression, regex or regexp is in theoretical computer science and
formal language theory.
✓ Disjunction
/cat|dog/
✓ Grouping
/pupp(y|ies)/
Regular Expressions
Advanced Operators
[a-z]*
[0-9]+ GO{2}D
BOOKS? GO{1,2}D
Operator Precedence Hierarchy
1. Parentheses ()
2. Counters * + ? {}
3. Sequence of Anchors ^my end$
4. Disjunction |
Regular Expressions
A Simple Example
/[tT]he/
/\b[tT]he\b/
/[^a-zA-Z][tT]he[^a-zA-Z]/
Regular Expressions
A More Complex Example
“Any PC with more than 500 MHz and 32 Gb of disk space for less than $1000”
/$[0-9]+/
/$[0-9]+\.[0-9][0-9]/
/\b$[0-9]+(\.[0-9][0-9])?\b/
Regular Expressions
A More Complex Example
Any PC with more than 500 MHz and 32 Gb of disk space for less than $1000”
/\b[0-9]+(MHz|[Mm]egahertz|GHz|[Gg]igahertz)\b/
/\b[0-9]+(\.[0-9]+)? (Gb|[Gg]igabytes?)\b/
/\b(Mac|Macintosh|Apple)\b/
/\b[0-9]+ (Mb|[Mm]egabytes?)\b/
Regular Expressions
Advanced Operators
\d{3}
\d{2,3}
Regular Expressions
Regular Expression Memory
NOT “The bigger they were, the faster they will be”
NOT “The bigger they were, the bigger they will be”
s/regexp1/regexp2/
s/([0-9]+)/<\1>/