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

Creation of self referencing regexp throws exception "bad regex reference" #8

Open
Ramambahara opened this issue Jan 4, 2018 · 3 comments

Comments

@Ramambahara
Copy link

Hi.
I'm trying to create composite regular expression by appending pieces one at a time.

Example:

#include <boost/xpressive/xpressive.hpp>

#include <iostream>
#include <string>

int main()
{
  using namespace boost::xpressive;
  using namespace std::string_literals;
  auto regex = sregex{ bos >> as_xpr("foo") >> *_ };

  regex = regex | sregex{ bos >> as_xpr("bar") >> *_ };

  std::cout << regex_match("foololo"s, regex) << std::endl;
  std::cout << regex_match("barlolo"s, regex) << std::endl;

  return 0;
}

I get exception from regex = regex | sregex{ bos >> as_xpr("bar") >> *_ };. It seems that inside of assignment it enters boost::xpressive::detail::regex_matcher::static_compile_impl2(...), does impl->tracking_clear(); and partially clears contents of the temporary object on the right hand side of assignment operator.

It seems to me that my case is pretty similar to the case described in the Embedding a Regex by Value example. I am able to reproduce this behavior for boost 1.65.1 and 1.66.0 on both MSVC 2015 sp1 with /std:c++=latest and gcc 7.2.0.

How should I create such composite regular expression assuming I don't have all the pieces at once?

@brightshine1111
Copy link

I've also run into this exact problem and I'm still hunting for a solution. Any help would be appreciated!

@brightshine1111
Copy link

I found a workaround. The issue seems to be with directly self-referencing a regex. If you copy a regex to a temporary variable first, you can then use that temp variable when updating the original regex. Example:

sregex rx1 = sregex::compile(".example");
sregex t = rx1;
rx1 = bos >> t >> eos;

I would guess there's some optimization in newer versions of gcc that's messing with the self-reference ability of the syntax.

@Ramambahara
Copy link
Author

Thank you a lot, it might also work for me!

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