
What the regex?! A Practical Guide to Regular Expressions
The hairiness of the regular expression syntax is mind-boggling but if you can master a few of the core concepts, you have another powerful tool in your toolbox.
1
2
donut
doughnut
1
do(ugh)?nut
- creating a search algorithm to find text on a page
- validating data like a URL format, telephone number, or password
- parsing a string into separate parts like an area code and a phone number
- replacing a generic error message with something more specific and on-brand
- searching your hard drive for files that mention a specific string
- searching in your IDE through files
- validating a markdown document format
|
operator will match either character or character sequence. For instance, if you wanted to test the user agent string to make sure it was an allowable user agent or to run customized logic for a specific user agent, you could use the boolean |
operator.1
iPhone|iPad|Android
1
2
3
iPhone
iPad
Android
1
2
BlackBerry
iPod
i
character at the end of the regex pattern (surrounded by /
).1
/iPhone|iPad|Android/i
1
2
3
iPhone
IPAD
anDROID
.
and matches any character except newlines.1
.+ing
1
2
3
4
boating
sailing
kayaking
surfing
1
2
3
4
boat
sail
kayak
surf
+
? We’ll cover that in the Quantifiers section.^
and $
respectively.https://
not http://
and ends with .com
.1
^https:\/\/.+\.com$
1
[https://jennapederson.com](https://jennapederson.com/)
1
2
http://jennapederson.com
https://jennapederson.dev
\
before the two /
and .
. We use the backslash \
to escape these metachacters to match the literal characters. The list includes [ \ ^ $ . | ? * + ( )
.+
? We’ll talk about that in the next section on Quantifiers!?
will match zero or one occurrence*
will match zero or more occurrences+
will match one or more occurrences{n}
will match exactly n occurrences{min,}
will match at least min occurrences{,max}
will match up to max occurrences{min,max}
will match between min and max occurrences1
\d{2}-\d{2}-\d{4}
1
^(\d{1,3})(\d{0,3})(\d{0,4})$
1
02-02-2020
1
12 May 2020
(
and )
to open and close a group in our pattern. Capturing groups lets us operate on them individually. These groups will be captured in an array and can be accessed by index.1
(\d{2})-(\d{2})-(\d{4})
1
02-02-2020
1
2
3
02
02
2020
\d
is a character class representing a larger set of characters, in this case, any digit. Other examples would be [0-9]
to represent any digit or [A-Z]
to represent any upper case letter.?<group name>
and access each group by it’s group name:1
(?<month>\d{2})-(?<day>\d{2})-(?<year>\d{4})