查找字母子字符串

find alphabetic substring

本文关键字:字符串 母子 查找      更新时间:2023-10-16

我有以下字符串,我只想从中提取大于1:的字母部分(字母子字符串)

  • %d.i.p.p.出席-->出席
  • 专业知识-->专业知识
  • n.c.p.c.condamner-->condamner

我正在尝试以下代码:

#include <regex>
#include <iostream>
void main()
{
    const std::string s = "% d. i.p.p. attendu";
    std::regex rgx("[a-zA-Z]{2,20}");
    std::smatch match;
    if (std::regex_search(s.begin(), s.end(), match, rgx))
        std::cout << "match: " << match[1] << 'n';
} 

但是我在运行代码时出现以下错误:抛出'std::regex_error'what():regex_error的实例后调用的Terminate

你能帮帮我吗,非常感谢。哈尼。

好的,我设法使用了boost,因为gcc的regex是一个可憎的

#include <boost/regex.hpp>
void main()
{
        const std::string s = "% d. i.p.p. tototo attendu";
        boost::regex re("[a-zA-Z]{4,7}");
        boost::smatch matches;
        if( boost::regex_search( s, matches, re ) )
         {
               std::string value( matches[0].first, matches[0].second );
                cout << value << "  ";
          }
}

很好,我找到了atteu,但输出只是tototo。它不是在增加

返回值是"tototo-attedu"我想知道是否可以一次返回每个值,而不是一个字符串

我想知道我是否可以一次返回每个值,而不是一个字符串

做到这一点的唯一方法似乎是通过regex_iterator。下面是一个使用Boost:的例子

#include <boost/regex.hpp>
#include <iostream>
int main() {
    const std::string s = "% d. i.p.p. tototo attendu";
    boost::regex rgx("([a-zA-Z]{2,20})");
    boost::smatch match;
    boost::sregex_iterator begin{s.begin(), s.end(), rgx},
                           end{};
    for (auto&& i = begin; i != end; ++i)
        std::cout << "match: " << *i << 'n';
}

这产生:

match: tototo
match: attendu

两件事:

  • main的返回类型为始终int。您的代码甚至不应该编译
  • 我在你的(第一个,这是正确的!)正则表达式周围添加了括号,这样它就为每个匹配创建了一个捕获。然后迭代器依次迭代每个匹配项