正则表达式。“[^”]*“
字符串 “lips“
结果: 匹配
字符串。“lips’“
结果:不匹配
我希望两个字符串都能匹配。
C++代码。
#include <iostream>
#include <string>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;
int main()
{
const string s1 = "“lips“";
const string s2 = "“lips’“";
if (regex_search(s1, regex("“[^”]*“"))) cout << "s1 matched" << endl;
if (regex_search(s2, regex("“[^”]*“"))) cout << "s2 matched" << endl;
return 0;
}
输出: s1匹配
符号是 ’
特别的?为什么第二个字符串不匹配?
utf-8引号和撇号有共同的字节,这就是为什么regex不能使用的原因。utf-8的代码。
#include <iostream>
#include <string>
#include <boost/regex.hpp>
#include <boost/regex/icu.hpp>
using namespace std;
using namespace boost;
int main()
{
const string s1 = "“lips“";
const string s2 = "“lips’“";
if (u32regex_search(s1, make_u32regex("“[^”]*“"))) cout << "s1 matched" << endl;
if (u32regex_search(s2, make_u32regex("“[^”]*“"))) cout << "s2 matched" << endl;
return 0;
}
编译: g++ -std=c++11 ./test.cc -licuuc -lboost_regex
输出:
s1 matched
s2 matched