In the example below, the regular expression corresponds to a single substring:
Here, we have matched two groups of hyphenated
ta-tu, each corresponding to this condition —
t and any character. A grouping without backreferencing will only find a whole expression but not two separate groups.
The rest of the characters in the string does not match. Among them are capital
T and a line break. We have not found:
Tbecause the character
tin the expression is in lowercase
We can modify the expression slightly to include the other substring from the example. To do this, we use a modifier.
Modifiers are characters specified after
? in a group of regular expression characters to change their behavior. If we put
?, it will ignore case sensitivity, and we get a match with another substring. But instead of lower case
t we have an upper case
Imagine we capitalize the second part of the substring after the hyphen in
Tu-tu. There would not be a match because the modifier only works within the group where we defined it:
So let us duplicate the
i modifier in the second group to get a match for
Tu-Tu in the string:
This entry is a shorter version of its counterpart:
We can also place modifiers in separate groups:
But in this case, we allocate our memory for four groups of matches.
Here we will look at another modifier —
s. It makes it so that periods include line breaks and carriage returns. We already know that periods do not include them by default. The substrings
tu-T have line breaks, so we do not process them.
We will put the modifier
s in the second group; now all the substrings are matched:
Modifiers can be disabled. All you have to do is put
- before them. Let's add
- to the first group and look at our example:
Also, we can combine active and disabled modifiers. We can add
s and disable
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.