Learn Regex the Easy Way, Part 6: Alternation and Grouping
Quick Recap #
In Part 5, we covered metacharacters, escaping with backslash, and the dot wildcard. We learned the 14 special characters and how to match them literally. Now let’s combine patterns with “or” logic and control what our quantifiers apply to.
The Pipe Means OR #
The pipe character | means “or” in regex. cat|dog matches either “cat” or “dog.”
Grouping with Parentheses #
Here’s where it gets critical. Without parentheses, the pipe applies to everything on each side:
cat|dog food # Matches "cat" OR "dog food"
# NOT "cat food" or "dog food"
With parentheses, you limit the scope:
(cat|dog) # Either "cat" or "dog"
[[:blank:]] # A space
food # Literal "food"
Compact: (cat|dog) food
MATCH THESE
cat fooddog foodDO NOT MATCH THESE
bird foodcatdogfood
Multiple Alternatives #
You can have as many alternatives as you need: (red|green|blue|yellow).
Grouping for Quantifiers #
Parentheses also let you apply quantifiers to multi-character sequences:
(ha)+ # One or more "ha"
# Matches: ha, haha, hahaha
Non-Capturing Groups #
Regular parentheses create a “capturing group” (we’ll cover capturing in Part 7). If you just want grouping without capturing, use (?:...). The ?: says “group this but don’t save it.”
(?:cat|dog) # Group without capturing
[[:blank:]] # Space
food # Literal "food"
Compact: (?:cat|dog) food
This behaves identically for matching. The difference only matters when you need capture groups (Part 7).
Practical Example: Image File Extensions #
^ # Start of line
[[:alnum:]_.-]+ # Filename characters
\. # Literal dot
(?:jpg|jpeg|png|gif|webp) # Image extensions
$ # End of line
Compact: ^[[:alnum:]_.-]+\.(?:jpg|jpeg|png|gif|webp)$
MATCH THESE
photo.jpglogo.pngbanner.webpDO NOT MATCH THESE
document.pdfphoto.jpg.bakscript.js
What to Practice #
- Write a regex that matches “http” or “https” (hint:
https?is one approach,http|httpsis another). - Write a regex matching US states: “Texas”, “California”, or “New York” as whole words.
- Write a regex for “ha”, “haha”, “hahaha” using
(ha)+with anchors. - What is the difference between
[cat]and(cat)? Write a sentence explaining it.
Definitions #
- Alternation (pipe |) - The OR operator in regex.
cat|dogmatches either “cat” or “dog.” - Capturing Group - Parentheses
(...)that save what they match for later reference. - Group - Parentheses that combine elements into a single unit for quantifiers or alternation.
- Nesting - Placing groups inside other groups:
(a(bc)d). - Non-Capturing Group (?:…) - Groups elements without saving the match. Uses
(?:...)syntax.
Series Navigation #
- Part 1: Make Regular Expressions the Easy Way
- Part 2: Anchors and Boundaries
- Part 3: Character Classes
- Part 4: Quantifiers
- Part 5: The Dot, Escaping, and Special Characters
- Part 6: Alternation and Grouping (this post)