0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>Let us see how we can use regular expressions to find similar character combinations.</p>
1
<p>Let us see how we can use regular expressions to find similar character combinations.</p>
2
<p>If we want to find the substrings gray or grow in the string, we need to use a mechanism called<strong>alternation</strong>:</p>
2
<p>If we want to find the substrings gray or grow in the string, we need to use a mechanism called<strong>alternation</strong>:</p>
3
<ul><li>We enter the first possible option</li>
3
<ul><li>We enter the first possible option</li>
4
<li>Then we add |</li>
4
<li>Then we add |</li>
5
<li>Then we enter the second option</li>
5
<li>Then we enter the second option</li>
6
</ul><p>We can see this match:</p>
6
</ul><p>We can see this match:</p>
7
<p>/gray|grow/</p>
7
<p>/gray|grow/</p>
8
<p>gray grow grey</p>
8
<p>gray grow grey</p>
9
<p>We can write the same condition more shortly. Since these substrings have a common part, we enter gr characters and then add an alternative using<strong>grouping</strong>:</p>
9
<p>We can write the same condition more shortly. Since these substrings have a common part, we enter gr characters and then add an alternative using<strong>grouping</strong>:</p>
10
<p>/gr(ay|ow)/</p>
10
<p>/gr(ay|ow)/</p>
11
<p>gray grow grey</p>
11
<p>gray grow grey</p>
12
<p>Grouping is really important here. If we remove it, the alternative to gray becomes ow:</p>
12
<p>Grouping is really important here. If we remove it, the alternative to gray becomes ow:</p>
13
<p>/gray|ow/</p>
13
<p>/gray|ow/</p>
14
<p>gray grow grey</p>
14
<p>gray grow grey</p>
15
<p>Below is another interesting example of using alternation. We spell this word gray in American English but grey in British English. To avoid missing any of the options in the text, we can use a<strong>concise alternative</strong>:</p>
15
<p>Below is another interesting example of using alternation. We spell this word gray in American English but grey in British English. To avoid missing any of the options in the text, we can use a<strong>concise alternative</strong>:</p>
16
<p>/gr(a|e)y/</p>
16
<p>/gr(a|e)y/</p>
17
<p>gray grow grey</p>
17
<p>gray grow grey</p>
18
<p>And we can simplify the resulting expression since the alternative uses single characters. Let us specify a character class that consists of the characters a and e:</p>
18
<p>And we can simplify the resulting expression since the alternative uses single characters. Let us specify a character class that consists of the characters a and e:</p>
19
<p>/gr[ae]y/</p>
19
<p>/gr[ae]y/</p>
20
<p>gray grow grey</p>
20
<p>gray grow grey</p>
21
<p>In this case, the regular expression mechanism, which job is to match things, works much more efficiently with character classes, especially single classes.</p>
21
<p>In this case, the regular expression mechanism, which job is to match things, works much more efficiently with character classes, especially single classes.</p>