mercredi 28 octobre 2020

Searching for a subsequence using Regular Expressions | C++ [duplicate]

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

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;
}

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++ version:

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.

The output is:

10001

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

Aucun commentaire:

Enregistrer un commentaire