Short answer
There is no such thing as a "position in the string that causes the
regular expression to fail".
However, I will show you an approach to answer the reverse question:
At which token in the regex did the engine become unable to match the
string?
Discussion
In my view, the question of the position in the string which caused the regular expression to fail
is upside-down. As the engine moves down the string with the left hand and the pattern with the right hand, a regex token that matches six characters one moment can later, because of quantifiers and backtracking, be reduced to matching zero characters the next—or expanded to match ten.
In my view, a more proper question would be:
At which token in the regex did the engine become unable to match the
string?
For instance, consider the regex ^w+d+$
and the string abc132z
.
The w+
can actually match the entire string. Yet, the entire regex fails. Does it make sense to say that the regex fails at the end of the string? I don't think so. Consider this.
Initially, w+
will match abc132z
. Then the engine advances to the next token: d+
. At this stage, the engine backtracks in the string, gradually letting the w+
give up the 2z
(so that the w+
now only corresponds to abc13
), allowing the d+
to match 2
.
At this stage, the $
assertion fails as the z
is left. The engine backtracks, letting the w+
, give up the 3
character, then the 1
(so that the w+
now only corresponds to abc
), eventually allowing the d+
to match 132
. At each step, the engine tries the $
assertion and fails. Depending on engine internals, more backtracking may occur: the d+
will give up the 2 and the 3 once again, then the w+
will give up the c and the b. When the engine finally gives up, the w+
only matches the initial a
. Can you say that the regex "fails on the "3"? On the "b"?
No. If you're looking at the regex pattern from left to right, you can argue that it fails on the $
, because it's the first token we were not able to add to the match. Bear in mind that there are other ways to argue this.
Lower, I'll give you a screenshot to visualize this. But first, let's see if we can answer the other question.
The Other Question
Are there techniques that allow us to answer the other question:
At which token in the regex did the engine become unable to match the
string?
It depends on your regex. If you are able to slice your regex into clean components, then you can devise an expression with a series of optional lookaheads inside capture groups, allowing the match to always succeed. The first unset capture group is the one that caused the failure.
Javascript is a bit stingy on optional lookaheads, but you can write something like this:
^(?:(?=(w+)))?(?:(?=(w+d+)))?(?:(?=(w+d+$)))?.
In PCRE, .NET, Python... you could write this more compactly:
^(?=(w+))?(?=(w+d+))?(?=(w+d+$))?.
What happens here? Each lookahead builds incrementally on the last one, adding one token at a time. Therefore we can test each token separately. The dot at the end is an optional flourish for visual feedback: we can see in a debugger that at least one character is matched, but we don't care about that character, we only care about the capture groups.
- Group 1 tests the
w+
token
- Group 2 seems to test
w+d+
, therefore, incrementally, it tests the d+
token
- Group 3 seems to test
w+d+$
, therefore, incrementally, it tests the $
token
There are three capture groups. If all three are set, the match is a full success. If only Group 3 is not set (as with abc123a
), you can say that the $
caused the failure. If Group 1 is set but not Group 2 (as with abc
), you can say that the d+
caused the failure.
For reference: Inside View of a Failure Path
For what it's worth, here is a view of the failure path from the RegexBuddy debugger.