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

Building regex by vector of others #24

Open
asergunov opened this issue Jul 8, 2022 · 0 comments
Open

Building regex by vector of others #24

asergunov opened this issue Jul 8, 2022 · 0 comments

Comments

@asergunov
Copy link

asergunov commented Jul 8, 2022

Hello.

I need to find which regex of N my string matches.

It's easy to implement if I have them compile time:

sregex re = (s1 = re1)|(s2 = re2)|(s3=re3);
...
std::set<int> foo(const std::string& str) {
 std::set<int> result;
 smatch match;
 if(regex_match(str, match, re)) {
 for(int i = 0; i<3; ++i)
  if(match[i+1]) result.insert(i);
 }
return result;
}

But I have a vector:

std::set<int> foo(const std::string& str, const std::vector<sregex>& reVector) {
 sregex re;
 int index = 1;
 for(auto&& item : reVector) {
   re |= (mark_tag(index++) = item); // (1)
 }

 std::set<int> result;
 smatch match;
 if(regex_match(str, match, re)) {
  for(int i = 0; i<reVector; ++i)
    if(match[i+1]) result.insert(i);
  }
 }
return result;
}

There at (1) |= doesn't work as I expect for some reson, It didn't catch some values.
If I replace it with

 re = re | (mark_tag(index++) = item);

it just crashes. If I replace with:

 auto t = re;
 re = t | (mark_tag(index++) = item);

it making captures of previous tags to be subexpresions matches. So code doesn't work as I want.

Is it any way to prevent sub-expression capture isolation?

As I understand it should be some intermediate type before sregex to express expression under construction. So it will not isolate matches of existing part.

I'm using v1.65.1

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

1 participant