Merge pull request #124 from josh-byster/alternation-fix

Clarified alternation
This commit is contained in:
Zeeshan Ahmad 2018-06-23 16:47:58 +04:00 committed by GitHub
commit aa7d432667
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -303,15 +303,16 @@ See also [4. Lookaround](# 4. Lookaround).
## 2.6 Alternation ## 2.6 Alternation
In regular expression Vertical bar `|` is used to define alternation. In a regular expression, the vertical bar `|` is used to define alternation.
Alternation is like a condition between multiple expressions. Now, you may be Alternation is like an OR statement between multiple expressions. Now, you may be
thinking that character set and alternation works the same way. But the big thinking that character set and alternation works the same way. But the big
difference between character set and alternation is that character set works on difference between character set and alternation is that character set works on
character level but alternation works on expression level. For example, the character level but alternation works on expression level. For example, the
regular expression `(T|t)he|car` means: uppercase character `T` or lowercase regular expression `(T|t)he|car` means: either (uppercase character `T` or lowercase
`t`, followed by lowercase character `h`, followed by lowercase character `e` or `t`, followed by lowercase character `h`, followed by lowercase character `e`) OR
lowercase character `c`, followed by lowercase character `a`, followed by (lowercase character `c`, followed by lowercase character `a`, followed by
lowercase character `r`. lowercase character `r`). Note that I put the parentheses for clarity, to show that either expression
in parentheses can be met and it will match.
<pre> <pre>
"(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. "(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.