Why cannot I find a number by regular expression?
Oct 11, 2014 at 6:41am UTC
Hello
Why cannot I find a number by regular expression?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <regex>
#include <string>
#include <iostream>
int main()
{
std::string str = "Hello my 5 compluter!" ;
try {
const std::regex e( "\d" , std::regex_constants::basic );
std::sregex_iterator next( str.begin(), str.end(), e );
std::sregex_iterator end;
while ( next != end ) {
std::smatch match = *next;
std::cout << match.str() << "\n" ;
next++;
}
} catch ( const std::regex_error &e ) {
std::cerr << "Error: incorrect regular expression" << std::endl;
return 1;
}
return 0;
}
Oct 11, 2014 at 7:03am UTC
It works for be if I remove the std::regex_constants::basic
argument and escape the backslash ('\d' is being interpreted as an escape code, like '\n').
Oct 11, 2014 at 7:26am UTC
Thank you very much! It works now!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <regex>
#include <string>
#include <iostream>
int main()
{
std::string str = "Hello my 5 compluter!" ;
try {
const std::regex e( "\\d" );
std::sregex_iterator next( str.begin(), str.end(), e );
std::sregex_iterator end;
while ( next != end ) {
std::smatch match = *next;
std::cout << match.str() << "\n" ;
next++;
}
} catch ( const std::regex_error &e ) {
std::cerr << "Error: incorrect regular expression" << std::endl;
return 1;
}
return 0;
}
Topic archived. No new replies allowed.