self.add(literal_bool, TokenId_Litera-6ren">
gpt4 book ai didi

c++ - 使用 boost::spirit::lex 的不区分大小写的关键字

转载 作者:搜寻专家 更新时间:2023-10-31 00:56:10 24 4
gpt4 key购买 nike

有没有办法不区分大小写地识别特定模式?

例如如果我有

literal_bool = L"True|False";
this->self.add(literal_bool, TokenId_LiteralBool);

如何匹配 trueTRUEtRuE 同时避免编写 [Tt][Rr][Uu][ Ee] 每个关键字?

最佳答案

Regular expressions supported by boost::spirit::lex包括区分大小写的控件:

(?r-s:pattern)

apply option 'r' and omit option 's' while interpreting pattern. Options may be zero or more of the characters 'i' or 's'. 'i' means case-insensitive. '-i' means case-sensitive. 's' alters the meaning of the '.' syntax to match any single character whatsoever. '-s' alters the meaning of '.' to match any character except '\n'.

因此你可以这样写:

literal_bool = L"(?i:true|false)";
this->self.add(literal_bool, TokenId_LiteralBool);

原始答案

引入一个使模式不区分大小写的函数:

literal_bool = L"True|False";
this->self.add(make_case_insensitive(literal_bool), TokenId_LiteralBool);

常规(非宽)字符串的实现:

std::string make_case_insensitive(const std::string& s)
{
std::string r;
std::string cC = "[xX]";
for(char c : s)
{
if ( std::isalpha(c) )
{
cC[1] = std::tolower(c);
cC[2] = std::toupper(c);
r += cC;
}
else
r += c;
}
return r;
}

关于c++ - 使用 boost::spirit::lex 的不区分大小写的关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40175448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com