Project

General

Profile

Actions

Bug #17774

open

Quantified empty group causes regex to fail

Added by Davidebyzero (David Ellsworth) about 3 years ago. Updated over 2 years ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-msys]
[ruby-core:103200]

Description

The regex ^((x*)(?=\2$))*x$ matches powers of 2 in unary, expressed as strings of x characters whose length is the number.

Adding an empty group () in the middle of it should have no effect on its operation, and indeed it does not. ^((x*)()(?=\2$))*x$ still matches powers of 2 just fine.
Quantifying that empty group, (){4}, should still have no effect. And indeed, ^((x*)(){4}(?=\2$))*x$ still matches powers of 2. But quantify that to (){5}, and suddenly it fails.

The following command line should print 1, but instead prints nothing:

ruby -e 'print 1 if "x"*32 =~ /^((x*)(){5}(?=\2$))*x$/'

However this one does print 1:

ruby -e 'print 1 if "x"*32 =~ /^((x*)(){4}(?=\2$))*x$/'

Bug found to occur on Try It Online: ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]
Bug confirmed to happen on my own machine: ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-msys]

Solving the challenge Is that number a Two Bit Number™️? on Code Golf Stack Exchange is what led me to discover this bug.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0