

However, the regex engine isn’t done yet. The capturing group took part in the match, so only the “then” part is used. Note that the “else” part is not attempted at this point. c fails to match d, and the match attempt fails. The capturing group took part in the match, so the “then” part or c is attempted. Like in the second string, the capturing group grabs the a and the b matches. The fourth subject abd is the most interesting one. The engine does try again starting at the second character in the string, but fails since b does not match c. d does not match c and the match attempt at the start of the string fails. The first capturing group did not take part in the match at all, so the “else” part or d is attempted. b still matches b, and the engine moves on to the conditional. Our third subject bc does not start with a, so the capturing group does not take part in the match attempt, like we saw with the first subject string. c matches c and an overall match is found. The regex engine again evaluates the conditional. Moving on to our second subject string abc, a matches a, which is captured by the capturing group. d matches d and an overall match is found. The regex engine now evaluates the conditional. In short: if you want to use a reference to a group in a conditional, use ( a ) ? instead of ( a ? ).Ĭontinuing with our regex, b matches b. Conditionals evaluating such groups execute the “then” part. Backreferences to a capturing group that took part in the match and captured nothing always succeed.

In the latter group, the capturing group always takes part in the match, capturing either a or nothing.

In the former regex, the capturing group does not take part in the match if a fails, and backreferences to the group will fail. Note that ( a ) ? is very different from ( a ? ). Any subsequent backreference to it like \1 will fail. Since the whole group was optional, the group did not take part in the match. Since the capturing group containing a is optional, the engine continues with b at the start of the subject string. Let’s see how this regular expression works on each of these four subject strings. It does not match bc, but does match bd in abd. The regex ( a ) ? b (?(1) c | d ) consists of the optional capturing group ( a ) ?, the literal b, and the conditional (?(1) c | d ) that tests the capturing group. Otherwise, there is no need to use parentheses around the then and else parts. If you want to use alternation, you will have to group the then or else together using parentheses, like in (? (?= condition ) ( then1 | then2 | then3 ) | ( else1 | else2 | else3 ) ). The number and the parentheses are part of the if-then-else syntax started with (?.įor the then and else, you can use any regular expression. Note that although the syntax for a conditional check on a backreference is the same as a number inside a capturing group, no capturing group is created. Place the number of the capturing group inside parentheses, and use that as the if part. If you use a lookahead as the if part, then the regex engine will attempt to match the then or else part (depending on the outcome of the lookahead) at the same position where the if was attempted.Īlternatively, you can check in the if part whether a capturing group has taken part in the match thus far. Remember that the lookaround constructs do not consume any characters. Because the lookahead has its own parentheses, the if and then parts are clearly separated. Using positive lookahead, the syntax becomes (? (?= regex ) then | else ). You may omit the else part, and the vertical bar with it.įor the if part, you can use the lookahead and lookbehind constructs. This part can be followed by a vertical bar and the else part. The opening bracket must be followed by a question mark, immediately followed by the if part, immediately followed by the then part. The syntax consists of a pair of parentheses. Otherwise, the else part is attempted instead. If the if part evaluates to true, then the regex engine will attempt to match the then part. If-Then-Else Conditionals in Regular ExpressionsĪ special construct (?ifthen|else) allows you to create conditional regular expressions.
