1
我只是切换到使用内置的正则表达式的功能regex.h(是跨平台)。并用以下简单的regex,许多有效的投入,如[email protected]现在失败(其中[email protected]仍然正常工作):与POSIX正则表达式奇怪的结果在Xcode
^[^@\s][email protected][^@\s]+\.[^\s\[email protected]]+$
调用代码:
return Common::regexMatch("^[^@\\s][email protected][^@\\s]+\\.[^\\s\\[email protected]]+$", userInput);
而这里的实现:
#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;
}
这是令人费解的是它基于不同的莱特在辨别r,当正则表达式模式根本没有字母规范。而且它不一样的投入是非常一致的。 TIA。
既然是'是失败了,您s',我想这与逃逸问题。尝试用单个替换双反斜杠。另外,没有必要在字符类中跳过这段时间。永远。 –
嘿感谢快速回复,这是超级有帮助的。原来POSIX扩展不支持转义空格字符,但只是字面上的空间很好,所以我更换了所有的“\\的”有“”,它发射的权利了! – leontx
注意,'\ s'具有比单一的空间不同的含义。 '\ s'等价于'[\ t \ n \ r]'。所以它匹配任何类型的空白。同样,随时将解决方案发布到您的问题上作为答案并接受它。如果你自己发现了这个问题,那是非常有效的。 –