Skip to content

Commit 000dd57

Browse files
committed
Add alternation
1 parent 9e17be3 commit 000dd57

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ contains uppercase letter and also it is too short.
2929
- [The Plus]()
3030
- [The Question Mark]()
3131
- [Character Group]()
32+
- [Alternation]()
3233
- [Quantifiers]()
3334
- [OR operator]()
3435
- [Character Sets]()
@@ -184,9 +185,21 @@ the comma the regular expression `[0-9]{2}` means: Match exactly 2 digits.
184185
Character group is a group of sub-pattern that is written inside Parentheses `(...)`. As we discussed before that in regular expression
185186
if we put quantifier after character than it will repeats the preceding character. But if we put quantifier after a character group than
186187
it repeats the whole character group. For example the regular expression `(ab)*` matches zero or more repetitions of the character "ab".
187-
We can also use the alternation inside character group. For example the regular expression `(c|g|p)ar` means: lowercase character `c`,
188+
We can also use the alternation `|` meta character inside character group. For example the regular expression `(c|g|p)ar` means: lowercase character `c`,
188189
`g` or `p`, followed by character `a`, followed by character `r`.
189190

190191
<pre>
191192
"(c|g|p)ar" => The <a href="#learn-regex"><strong>car</strong></a> is <a href="#learn-regex"><strong>par</strong></a>ked in the <a href="#learn-regex"><strong>gar</strong></a>age.
193+
</pre>
194+
195+
## 2.5 Alternation
196+
197+
In regular expression Vertical bar `|` is used to define alternation. Alternation is like a condition between multiple expressions. Now,
198+
you maybe thinking that character set and alternation works the same way. But the big difference between character set and alternation
199+
is that character set works on character level but alternation works on expression level. For example the regular expression
200+
`[T|t]he|car` means: uppercase character `T` or lowercase `t`, followed by lowercase character `h`, followed by lowercase character `e`
201+
or lowercase character `c`, followed by lowercase character `a`, followed by lowercase character `r`.
202+
203+
<pre>
204+
"[T|t]he|car" => <a href="#learn-regex"><strong>The</strong></a> <a href="#learn-regex"><strong>car</strong></a> is parked in <a href="#learn-regex"><strong>the</strong></a> garage.
192205
</pre>

0 commit comments

Comments
 (0)