Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Top-level recursion ignores top-level alternatives #177

Open
Davidebyzero opened this issue Jul 16, 2022 · 1 comment
Open

Top-level recursion ignores top-level alternatives #177

Davidebyzero opened this issue Jul 16, 2022 · 1 comment

Comments

@Davidebyzero
Copy link

Davidebyzero commented Jul 16, 2022

A top-level recursive call, i.e. (?R) or (?0), only uses the first alternative, and ignores all subsequent ones.

As a simple example, a(?R)|z will only match z, but it is supposed to be able to also match az. The workaround is to use (?:a(?R)|z) or (a(?R)|z) or (a(?1)|z), which can match either az or z.

The regex which actually led to discovery of this bug was (?=(xx+?)\1*(?=\1$)((?R)))(?=(x+)\3*(?=\3$)((?R)))\2\4|x\B(?R)|, which calculates the OEIS A064097 number sequence in unary. But to make it work in the current version of Boost, the entire regex must be enclosed in parentheses.

I have tested and confirmed this to be happening in the latest version of Notepad++, v8.4.4 (released yesterday), which uses presumably the latest (or at least a very recent) version of Boost as its regex engine.

Sample program demonstrating the bug:

#include <boost/regex.hpp>
#include <iostream>
int main()
{
    boost::smatch what;
    if (boost::regex_search(std::string("az"), what, boost::regex(   "a(?R)|z" ))) std::cout << what[0] << '\n';
    if (boost::regex_search(std::string("az"), what, boost::regex("(?:a(?R)|z)"))) std::cout << what[0] << '\n';
    if (boost::regex_search(std::string("az"), what, boost::regex(  "(a(?1)|z)"))) std::cout << what[0] << '\n';
    return 0;
}

This should print three identical lines of az, but instead prints z followed by two lines of az.

@jzmaddock
Copy link
Collaborator

Thanks for the report, will investigate shortly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants