GROUPS & ALTERNATION

Grouping

Parentheses bundle a sub-pattern so operators apply to all of it.

A quantifier normally applies to one element. ab+ is a, then one or more b - it matches "ab", "abbb". Wrap a group and the quantifier applies to the whole thing: (ab)+ is one or more repetitions of ab, so it matches "ab", "abab", "ababab".

Groups also contain alternation: gr(a|e)y is gray or grey, and ^(cat|dog)$ is a line that is exactly cat or dog. When you only want the bundling and not a stored capture, reach for a non-capturing group.

PRACTICE - 2 DRILLS 0/2 DONE
DRILL 1/2

Match every unbroken run of repeated "ab".

/ /
ababab then ab
must match: "ababab" "ab"
aabb
must match: "ab"
acb
must match nothing
DRILL 2/2- Batman

Match each unbroken run of repeated "na".

/ /
nanana batman
must match: "nanana"
na na na
must match: "na" "na" "na"
hello
must match nothing