mardi 26 février 2019

python regex findall convert to c++ regex [duplicate]

This question already has an answer here:

my python code is:

import re
regex1 = r"\[P\] .+? \[\/P\]"
regex2 = r"\[P\] (.+?) \[\/P\]"
line = "President [P] Barack Obama [/P] met Microsoft founder [P] Bill Gates [/P], yesterday."
person1 = re.findall(regex1, line)
person2 = re.findall(regex2, line)
print(person1)
print(person2)

The result is:
person1:['[P] Barack Obama [/P]', '[P] Bill Gates [/P]']
person2:['Barack Obama', 'Bill Gates']

and I want to convert the python code to c++.
my case c++ code is below:

  const std::string s = "President [P] Barack Obama [/P] met Microsoft founder [P] Bill Gates [/P], yesterday.";

  std::regex words_regex("\\[P\\] (.+?) \\[/P\\]+?");
  auto words_begin = std::sregex_iterator(s.begin(), s.end(), words_regex);
  auto words_end = std::sregex_iterator();
  for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
    std::smatch match = *i;
    std::string match_str = match.str();
    std::cout << match_str << '\n';
  }

and the result is:
[P] Barack Obama [/P] [P] Bill Gates [/P] and my question how to remove the [p]and[/p] and I want get the result like person2 above?

Aucun commentaire:

Enregistrer un commentaire