使用正则表达式搜索子序列|C ++ [英] Searching for a subsequence using Regular Expressions | C++

查看:64
本文介绍了使用正则表达式搜索子序列|C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在字符串中搜索一个以0开头的序列,以1开头和结尾.例如,

I would like to search for a sequence of 0s inside my string, starting and ending with 1. For example,

对于100001功能应打印:100001用于1000101功能的应打印:10001和101

for 100001 function should print out: 100001 for 1000101 function should print out: 10001 and 101

我试图使用正则表达式来完成它,但是我的代码未能做到这一点.

I tried to accomplish it using regular expressions, but my code fails to do so.

#include <iostream>
#include <regex>



int main(int argc, char * argv[]){

     std::string number(argv[1]);
     std::regex searchedPattern("1?[0]+1");

     std::smatch sMatch;

     std::regex_search(number,sMatch,searchedPattern);

     for(auto& x : sMatch){
         std::cout << x << std::endl;
     }

     return 0;
}

我用于在Linux(Ubuntu版本18.04)上编译代码的命令:

The command, that I'm using to compile the code on the Linux(Ubuntu version 18.04):

g++ Cpp_Version.cpp -std=c++14 -o exec
./exec 1000101

g ++版本:

g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

输出为:

10001

我质疑我的模式是错误的.有什么想法可以改善它吗?

I quess that my pattern is wrong. Any ideas how to improve it?

推荐答案

std :: regex_search 不会搜索所有结果.请使用 std :: sregex_iterator .其文档状态(强调我的意思):

std::regex_search does not search for all of the results. Use std::sregex_iterator instead. Its documentation states (emphasis mine):

在构建过程中, 以及每增加一个 ,它就会调用 std :: regex_search

#include <iostream> // std::cout, std::cerr
#include <regex> // std::regex, std::smatch, std::regex_search, std::sregex_iterator
#include <cstdlib> // EXIT_FAILURE, EXIT_SUCCESS

int main(int argc, char **argv) {
    if (argc < 2) {
        std::cerr << "./a.out 1000101" << std::endl;
        return EXIT_FAILURE;
    }
    std::string n{argv[1]};
    std::regex p{"(?=(1[0]+1))"};
    std::smatch m;
    if (false == std::regex_search(n, m, p)) {
        std::cerr << "regex_search has no match!" << std::endl;
        return EXIT_FAILURE;
    }
    std::cout << "regex_search found " << m.size() << " matches! But this is misleading...\n";
    for (const auto & field : m) {
        const auto begin = std::distance(n.cbegin(), field.first);
        const auto end = begin + std::distance(field.first, field.second);
        std::cout
            << "[" << begin << "," << end << "]\t"
            << field << "\n";
    }
    std::cout << "Unfortunately `sregex_iterator` can't tell you how many matches.\n";
    for (std::sregex_iterator it{n.cbegin(), n.cend(), p}, end{}; it != end; ++it) {
        m = *it;
                // m[0] is the capture for the lookahead. it is always empty, but it is needed to have an overlapping match group.
                // m[1] is the capture of your param.
        for (const auto & field : m) {
            const auto begin = std::distance(n.cbegin(), field.first);
            const auto end = begin + std::distance(field.first, field.second);
            std::cout
                << "[" << begin << "," << end << "]\t"
                << field << "\n";
        }
    }
    return EXIT_SUCCESS;
}

这是输出:

$ g++ --version
g++ (GCC) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -std=c++20 -O2 -Wall -pedantic example.cpp && ./a.out 1000100101
regex_search found 2 matches! But this is misleading...
[0,0]
[0,5]   10001
Unfortunately `sregex_iterator` can't tell you how many matches.
[0,0]
[0,5]   10001
[4,4]
[4,8]   1001
[7,7]
[7,10]  101

这篇关于使用正则表达式搜索子序列|C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆