Let us see how we can use regular expressions to find similar character combinations.
If we want to find the substrings
grow in the string, we need to use a mechanism called alternation:
We can see this match:
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 grouping:
Grouping is really important here. If we remove it, the alternative to
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 concise alternative:
And we can simplify the resulting expression since the alternative uses single characters. Let us specify a character class that consists of the characters
In this case, the regular expression mechanism, which job is to match things, works much more efficiently with character classes, especially single classes.
The Hexlet support team or other students will answer you.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
Sign up or sign in
Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.