1

I just switched to the built-in regex functionality using regex.h (to be cross platform). And with the following simple regex, many valid inputs, such as as@abc.com now fail (where ab@abc.com still works fine):

^[^@\s]+@[^@\s]+\.[^\s\.@]+$

The calling code is:

return Common::regexMatch("^[^@\\s]+@[^@\\s]+\\.[^\\s\\.@]+$", userInput);

And here's the implementation:

#import "regex.h"

bool Common::regexMatch(const string& regex, const string& text) {
   //return [[NSString stringWithCString:text.c_str() encoding:NSUTF8StringEncoding] isMatchedByRegex:[NSString stringWithCString:regex.c_str() encoding:NSUTF8StringEncoding]];

   regex_t re = {0};
   if (regcomp(&re, regex.c_str(), REG_NOSUB | REG_EXTENDED | REG_ICASE) != 0)
      return false;
   int status = regexec(&re, text.c_str(), (size_t)0, NULL, 0);
   regfree(&re);
   if (status != 0)
      return false;
   return true;   
}

It's baffling that it's discriminating based on a different letter, when the regex pattern has no letter specifications in it at all. And it's very consistent on which inputs it doesn't like. TIA.

leontx
  • 1,165
  • 1
  • 14
  • 24
  • 1
    Since it is `s` that is failing you, I suppose it's a problem with the escaping. Try replacing the double backslashes with single ones. Also, there is no need to escape the period inside a character class. Ever. – Martin Ender Nov 13 '12 at 01:52
  • Hey thanks for the quick reply, that was super helpful. It turns out POSIX Extended doesn't support escaping the space character, but is just fine with literal spaces, so I replaced all of the "\\s" with " " and it fired right up! – leontx Nov 13 '12 at 02:05
  • note that `\s` has a different meaning than a single space. `\s` is equivalent to `[\t\n\r ]`. So it matches any kind of whitespace. also, feel free to post the solution to your question as an answer and accept it. that's perfectly valid, if you've found the problem yourself. – Martin Ender Nov 13 '12 at 02:07
  • I just found this great post http://stackoverflow.com/a/400316/497357 that with POSIX Extended syntax, you cannot escape ANY characters within a character class. – leontx Nov 13 '12 at 02:40

1 Answers1

0

It turns out POSIX Extended doesn't support escaping the space character, but is just fine with literal spaces, so I replaced all of the "\s" with " " and it fired right up.

For more info, see https://stackoverflow.com/a/400316/497357

Community
  • 1
  • 1
leontx
  • 1,165
  • 1
  • 14
  • 24