regex-crossword-solver icon indicating copy to clipboard operation
regex-crossword-solver copied to clipboard

Error in repeated capturing group

Open ysard opened this issue 2 years ago • 2 comments

Hi, I encounter a problem with this not supported regex syntax:

^(A|B){2}-\1$

The parser fails silently until the solver returns None. I don't know how to tweak the grammar to support this.

The question is: What should match a back reference to a capture group followed by a quantifier ? What character should end the string AB- ? A, B or AB ?

Following the advise here: https://regex101.com/

A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data.

So the capture should be the content of the last repetition, so in the example, it should be AB-B.

Python implementation is ok with this:

import re
assert re.match(r"^(A|B){2}-\1$", "AB-A") is None
assert re.match(r"^(A|B){2}-\1$", "AB-B").groups() == ('B',)

ysard avatar Jan 12 '22 03:01 ysard