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

Alternation and grouping - Bash Tutorial

From the course: Bash Patterns and Regular Expressions

Start my 1-month free trial

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…

Contents