At which position does the regex fail?

regex, ruby, string, substring, syntax

Solution

An idea: what you can do is to tokenize your pattern and write it with optional nested capturing groups:

^(Q+(E+(R+($)?)?)?)?

Then you only need to count the number of capture groups you obtain to know where the regex engine stops in the pattern and you can determine the offset of the match end in the string with the whole match length.

As @zx81 notices it in his comment, if one of the elements can match the next element (example Q can match the element E), things become different.

Let's say that Q is `\w` (and can match E and R). For the string `QQQEEERRR` the precedent pattern will give only one capturing group (the greedy `\w+` matches all) when `^(\w+)(E+)(R+)$` will give three groups: `QQQEE`, `E`, `RRR`

To obtain the same result you need to add an alternation:

^((?:\w+(?=E)|\w+)(E+(R+($)?)?)?)?

In the alternation, the case where E exists must be tested first, and only if this branch fails (with the lookahead), then the other branch where E doesn't exist is used.

Thus the full pattern can be rewritten like this to deal with this specific case:

^((?:Q+(?=E)|Q+)((?:E+(?=R)|E+)((?:R+(?=$)|R+)($)?)?)?)?

Perhaps could you take a look to the gem amatch too.

Problem

I need a very simple string validator that would show where is first symbol not corresponding to the desired format. I want to use regex but in this case I have to find the place where the string stops corresponding to the expression and I can't find a method that would do that. (It's got to be a fairly simple method... maybe there isn't one?) For example if I have regex: ``` /^Q+E+R+$/ ``` with string: ``` "QQQQEEE2ER" ``` The desired result should be 7

Original source