简体   繁体   English

C ++正则表达式匹配字符串

[英]C++ regular expression to match a string

I'm a little poor with regular expressions so I would appreciate help if someone can tell me what the right regular expression would be to capture the three elements that are in this format - 我对正则表达式有点不满意,所以如果有人可以告诉我正确的正则表达式将捕获这种格式的三个元素,那么我将不胜感激-

<element1>[<element2>="<element3>"]

I could use boost if needed. 如果需要,我可以使用Boost。 The delimiters in this string are '[', '=', ']', '"' and ' ' . 该字符串中的分隔符为'[', '=', ']', '"'' '

Update : This is what I tried till now - 更新 :这是我到目前为止尝试过的-

int main(void) {

   std::string subject("foo[bar=\"baz\"]");
   try {
      std::regex re("([a-zA-Z]+)[([a-zA-Z])=");
      std::sregex_iterator next(subject.begin(), subject.end(), re);
      std::sregex_iterator end;
      while (next != end) {
         std::smatch match = *next;
         std::cout << match.str() << std::endl;
         next++;
      }
   } catch (std::regex_error& e) {
      std::cout << "Error!" << std::endl;
   }
}

Though this give me - 虽然这给了我-

foo[
bar
baz

Thanks 谢谢

You don't need iterators for this, you can match it all in one expression with capture groups (<capture>) that return sub matches like this: 您不需要使用迭代器,可以在一个表达式中将其与捕获组(<capture>)进行匹配,这些捕获组返回子匹配,如下所示:

// Note: Raw string literal R"~()~" removes the need to escape the string
std::regex const e{R"~(([^[]+)\[([^=]+)="([^"]+)"\])~"}; 
//                     ^  1  ^  ^  2  ^  ^  3  ^
//                     |     |  |     |  |_____|------- sub_match #3
//                     |     |  |     |
//                     |     |  |_____|---------------- sub_match #2
//                     |     |
//                     |_____|------------------------- sub_match #1

std::string s(R"~(foo[bar="baz"])~"); // Raw string literal again

std::smatch m;

if(std::regex_match(s, m, e))
{
    std::cout << m[1] << '\n'; // sub_match #1
    std::cout << m[2] << '\n'; // sub_match #2
    std::cout << m[3] << '\n'; // sub_match #3
}

You could use \\[<\\[" \\]?(\\[^<>\\[\\]" =\\x0a\\x0d\\]+)\\[>\\[" \\]? to get the elements: 您可以使用\\[<\\[" \\]?(\\[^<>\\[\\]" =\\x0a\\x0d\\]+)\\[>\\[" \\]?获得元素:

#include <string>
#include <sstream>
#include <vector>
#include <iterator>
#include <regex>
#include <iostream>
#include <iomanip>

auto input_text{
R"(foo[bar="baz"]
<element1>[<element2>="<element3>"])"};

auto fromString(std::string str) {
    std::vector<std::string> elements;

    std::regex r{R"([<\[" ]?([^<>\[\]" =\x0a\x0d]+)[>\[" ]?)"};
    std::istringstream iss(str);
    auto it = std::sregex_iterator(str.begin(), str.end(), r);
    auto end = std::sregex_iterator();
    for(; it != end; ++it) {
        auto match = *it;
        auto element = match[1].str();
        elements.push_back(element);

    }
    return elements;
}

int main()
{
    auto result = fromString(input_text);
    for (auto t : result) {
        std::cout << t << '\n';
    }

    return 0;
}

Output: 输出:

foo
bar
baz
element1
element2
element3

Live demo 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM