为什么 C++17 个字符类 [:blank:] 匹配换行符和回车符



>在Visual Studio 2015中使用POSIX字符类[:blank:]时,我希望换行符和回车符不匹配,但这不是我看到的行为。

http://www.cplusplus.com/reference/regex/ECMAScript/表示 POSIX 字符类[:blank:]等效于isblank但它们没有排队。 isblank为空格和制表符返回正值,但为回车符和换行符返回 false,而[:blank:]则匹配空格、制表符、换行符和回车符。

这是我的示例代码:

#include <cctype>
#include <iostream>
#include <regex>
#include <string>
using namespace std;
bool reMatches( const regex &re, const char c )
{
    return regex_search( string( 1, c ), re );
}
bool isBlank( const char c )
{
    return isblank( c ) != 0;
}
int main() {
    const char testChars[] = { ' ', 't', 'r', 'n', '.' };
    const char *dispChars[] = { " ", "\t", "\r", "\n", "." };
    regex explicitClass( "[ t]" );
    regex posixClass( "[[:blank:]]" );
    for ( unsigned i = 0; i < sizeof( testChars ); ++i )
    {
        char c = testChars[i];
        if ( isBlank( c ) != reMatches( explicitClass, c ) )
            cout << "Mismatch found between isblank & [ t] for " << dispChars[i] << std::endl;
        if ( isBlank( c ) != reMatches( posixClass, c ) )
            cout << "Mismatch found between isblank & [[:blank:]] for " << dispChars[i] << std::endl;
    }
}

下面是生成的输出:

Mismatch found between isblank & [[:blank:]] for r
Mismatch found between isblank & [[:blank:]] for n

[ t]按预期行事,但[[:blank:]]匹配rn!我做错了什么吗?

事实证明,这是VC++中的一个错误。在 clang 或 gcc 下运行此代码会表现出正确的行为(无输出(。

代码测试:http://rextester.com/MCP44517
错误提交: https://connect.microsoft.com/VisualStudio/feedback/details/3131111/std-regex-posix-character-class-blank-matches-newlines

最新更新