It resembles a ternary operator from programming languages and looks like this:
Let's look at an example of how it works:
We see outside brackets with
?, and inside are two separate expressions:
(?<=a), which checks if the character on the left matches
pdepending on whether the condition worked or not.
You can describe this construction as follows: “Find all instances of
m that are preceded either by an
a or by a
p preceded by no
Let's break down the search in our string character by character:
acomes before it. Since the condition hasn't been met, it'll attempt to extract
p, which is also not the current character;
adoesn't fit the conditions, the missing substring
m, previously was
a— match was found;
,does not fit the conditions, the missing substring
mam,, then it is checked
pdoes not correspond to
am, but corresponds to
p— a second match is found;
aagain triggers the check for the next character to match
That's how we got two matches in the string as a result of the conditional search.
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.