2

I would like to install a condition. I have already tried a lot but without success. Would someone be so kind and would help me?

My goal: if SharedKeys true then match elements(A B C D).

SharedKeys = "A","B","C","D"

Regex: (?!\")[A-Z]*?(?=\")

Match elements: A B C D

Update: (SharedKeys)?(?(1)|(?!\")[A-Z]*?(?=\"))

Match elements: SharedKeys A B C D

Link: https://regex101.com/r/WFxZh4/1

I think i have now what i needed, maybe helps others.

SharedKeys = "A","B","C","D"

BlaBla = "B", "B", "C"

Result: (SharedKeys|BlaBla)?(?(1)|(?!\")[A-Z]*?(?=\"))

Match elements: SharedKeys A B C D BlaBla B B C

Result for c++: [A-Za-z]+|(?!\")[A-Z]*?(?=\") (std::regex)

Lendoria
  • 127
  • 1
  • 11

1 Answers1

1

with std::regex:

std::string s1( R"(SharedKeys = "A","B","C","D")" );
std::regex rx( "(SharedKeys)?(?(1)|[A-Z](?=\\\"))" );
std::match_results< std::string::const_iterator > mr;

while( std::regex_search( s1, mr, rx ) ){
    std::cout << mr.str() << '\n';
    s1 = mr.suffix().str();
}

the output:

terminate called after throwing an instance of 'std::regex_error'  
what():  Invalid special open parenthesis. Aborted (core dumped)

with boost::regex:

std::string s1( R"(SharedKeys = "A","B","C","D")" );
boost::regex rx( "(SharedKeys)?(?(1)|[A-Z](?=\\\"))" );
boost::match_results< std::string::const_iterator > mr;

while( boost::regex_search( s1, mr, rx ) ){
    std::cout << mr.str() << '\n';
    s1 = mr.suffix().str();
}

the output:

SharedKeys
A
B
C
D

here you can see the difference between this two flavors:

boost_regex_table

source of the screenshot


Note. The std::regex is buggy especially with gcc version 6.3.0 and lower versions

Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44