fork(1) download
  1. #include <iostream>
  2. #include <regex>
  3. #include <string>
  4. #include <experimental/optional>
  5.  
  6. std::experimental::optional<int> find_int_regex( const std::string & s )
  7. {
  8. static const std::regex r( "(\\d+)" );
  9. std::smatch match;
  10. if( std::regex_search( s.begin(), s.end(), match, r ) )
  11. {
  12. return std::stoi( match[1] );
  13. }
  14. return {};
  15. }
  16.  
  17. int main()
  18. {
  19. for( std::string line; std::getline( std::cin, line ); )
  20. {
  21. auto n = find_int_regex( line );
  22. if( n )
  23. {
  24. std::cout << "Got " << n.value() << " in " << line << std::endl;
  25. }
  26. }
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0s 15344KB
stdin
hello 123-abc
cruel
world 456-def
stdout
Got 123 in hello 123-abc
Got 456 in world 456-def