Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching single digit with std::regex_match

Tags:

c++

regex

According to this reference, I should be able to match a single digit with

std::regex e1 ("\\d");  

However, when I run the following test code I get a regex exception.

#include <iostream>
#include <string>
#include <regex>

int main()
{
  std::regex r("\\d");
  std::string s("9");
  if (std::regex_match(s, r)) { std::cout << "matched!" << std::endl; }
}
like image 933
onezeno Avatar asked Dec 11 '13 17:12

onezeno


People also ask

How do I match a number in regex?

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. That's the easy part. Matching the three-digit numbers is a little more complicated, since we need to exclude numbers 256 through 999.

How do you represent a digit in regex?

1.2 Example: Numbers [0-9]+ or \d+ It matches any SINGLE character in the list. In this example, [0-9] matches any SINGLE character between 0 and 9 (i.e., a digit), where dash ( - ) denotes the range.

Can you use regex in C++?

The C++ standard library supports multiple regular expression grammars.

What does match in regex?

Match(String) Searches the specified input string for the first occurrence of the regular expression specified in the Regex constructor. Match(String, Int32) Searches the input string for the first occurrence of a regular expression, beginning at the specified starting position in the string.


2 Answers

GCC's std::regex support is not yet ready for prime time. See: Is gcc 4.8 or earlier buggy about regular expressions?

like image 149
Tim Pierce Avatar answered Oct 04 '22 02:10

Tim Pierce


If std::regex support is still buggy as @qwrrty suggests, the character class '[0-9]' is a substitute for '\d'.

like image 42
skelliam Avatar answered Oct 04 '22 00:10

skelliam