In the example below, the regular expression corresponds to a single substring:
Here, two groups of hyphenated
ta-tu characters are matched, each corresponding to this condition: "
t and any character". A grouping without backreferencing will only find an expression in its entirety, not two separate groups. The rest of the characters in the string aren't matched. Among them are capital
T and line break. Capital
T isn't found because the character
t in the expression is in lower case, and the second isn't found because periods by default don't include a line break.
We can modify the expression slightly so that it includes the other substring from the example. To do this, we'll use a modifier.
Modifiers are characters that are specified after
? in a group of regular expression characters and that change their behavior. If we put
?, it makes it ignore case sensitivity, and we get get a match with another substring, but instead of lower case
t, we have an upper case
If we capitalize the second part of the substring after the hyphen in
Tu-tu, there won't be a match because the modifier only works within the group it's defined in.
So let's duplicate the
i modifier in the second group and get a match for
Tu-Tu in the string:
This entry is a shorter version of its counterpart:
Modifiers can also be placed in separate groups:
But in this case, memory will be allocated for 4 groups of matches.
Let's look at another interesting modifier
s. It makes it so that periods include line breaks and carriage returns. We already know that by default, periods don't include them, and we were able to check this at the beginning of the lesson.
tu-T are followed by a line break, so these substrings aren't matched in the string. Let's 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
-. in front of them. Let's add
- to the first group and look at our example:
Active and disabled modifiers can be combined. 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:
From a novice to a developer. Get a job or your money back!
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.