From the course: Bash Patterns and Regular Expressions
Unlock the full course today
Join today to access over 22,700 courses taught by industry experts or purchase this course individually.
Alternation and grouping - Bash Tutorial
From the course: Bash Patterns and Regular Expressions
Alternation and grouping
- Alternation allows us to specify one thing or another. Grouping allows combining patterns into one match. This is important because we can use alternation to match one or another group, or we can use occurrence operands to specify how many times we should match the entire group. This might not seem like it's that important, but using alternation, expression groups and specifying occurrences can be quite powerful. Let's start with a basic example. To specify dog or cat separate them with a pipe symbol. This will match both dog and cat or even multiple occurrences of both, if we didn't specify an anchor, or occurrence operands, due to concatenation and greedy matches. However, the syntax is the foundation of alternation. If you want to specify the number of occurrences, we'd place the regex inside of a parentheses and then specify an occurrence operand. For instance, if you wanted to match zero or one dog or cat we'd use caret, left parenthesis, dog, pipe, cat, right parenthesis…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
What are regular expressions?2m 35s
-
(Locked)
Why aren't regexes consistent?1m 30s
-
(Locked)
Basic vs. Extended Regular Expressions3m 38s
-
(Locked)
Regex support in command line tools57s
-
(Locked)
Matching characters and words3m 21s
-
(Locked)
Specifying occurrences1m 56s
-
(Locked)
Alternation and grouping3m 21s
-
(Locked)
Back references and subexpressions1m 48s
-
(Locked)
-
-
-
-
-