-1
regex_t reg;
int value;
int return_value;
value = regcomp(&reg, "^[2-9]+#*", REG_EXTENDED);
if (value == 0){
    return_value = regexec(&reg, nums, 0, NULL, 0);
}

When I pass in nums = "2 23", with that space in the middle, the regexec returns a 0, or a match. Confused as to how to correct the regex or the C code itself to fix this.

I want the regex to be that so that only digits 2-9 are accepted, followed by any number of pound signs. No other symbols, or no pound signs at the beginning.

1 Answers1

0

Your regex is matching the digit 2, followed by zero pound signs, at the beginning of the subject string. You can see this for yourself by supplying one regmatch_t object to regexec via its third and fourth arguments; the values written to this object will indicate that the regex matched only the first character of the string.

If you want to match only when the entire string matches, add a $ to the end of your regex. This changes the meaning of the regex to "match the beginning of the string, followed by one or more digits 2-9, followed by zero or more pound signs, followed by the end of the string."

zwol
  • 121,956
  • 33
  • 219
  • 328