[英]My last regular expression won't work but i cannot figure out the reason why
I have two vectors, one which holds my regular expressions and one which holds the string in which will be checked against the regular expression, most of them work fine except for this one (shown below) the string is a correct string and matches the regular expression but it outputs incorrect instead of correct. 我有两个向量,一个向量保存我的正则表达式,另一个向量保存将要在正则表达式中进行检查的字符串,除了这一个(如下所示)之外,大多数向量都可以正常工作,该字符串是正确的字符串并且与正则表达式匹配表达式,但输出不正确而不是正确。
INPUT STRING 输入字符串
.C/IATA
CODE IS BELOW 代码如下
std::string errorMessages [6][6] = {
{
"Correct Corparate Name\n",
},
{
"Incorrect Format for Corporate Name\n",
}
};
std::vector<std::string> el;
split(el,message,boost::is_any_of("\n"));
std::string a = ("");
for(int i = 0; i < el.size(); i++)
{
if(el[i].substr(0,3) == ".C/")
{
DCS_LOG_DEBUG("--------------- Validating .C/ ---------------");
output.push_back("\n--------------- Validating .C/ ---------------\n");
str = el[i].substr(3);
split(st,str,boost::is_any_of("/"));
for (int split_id = 0 ; split_id < splitMask.size() ; split_id++ )
{
boost::regex const string_matcher_id(splitMask[split_id]);
if(boost::regex_match(st[split_id],string_matcher_id))
{
a = errorMessages[0][split_id];
DCS_LOG_DEBUG("" << a )
}
else
{
a = errorMessages[1][split_id];
DCS_LOG_DEBUG("" << a)
}
output.push_back(a);
}
}
else
{
DCS_LOG_DEBUG("Do Nothing");
}
st[split_id] = "IATA" st [split_id] =“ IATA”
splitMask[split_id] = "[a-zA-Z]{1,15}" <--- splitMask [split_id] =“ [a-zA-Z] {1,15}” <---
But it still outputs Incorrect format for corporate name 但是它仍然输出不正确的公司名称格式
I cannot see why it prints incorrect when it should be correct can someone help me here please ? 我看不到为什么它应该正确打印时打印不正确的原因,有人可以在这里帮助我吗?
Your regex and the surrounding logic is OK. 您的正则表达式和周围的逻辑都可以。
You need to extend your logging and to print the relevant part of splitMask
and st
right before the call to boost::regex_match
to double check that the values are what you believe they are. 您需要扩展日志记录,并在调用
boost::regex_match
之前,先打印splitMask
和st
的相关部分,以仔细检查这些值是否符合您的splitMask
。 Print them surrounded in some punctuation and also print the string length to be sure. 将它们打印在标点符号的周围,并确保确定字符串的长度。
As you probably know, boost::regex_match
only finds a match if the whole string is a match; 您可能知道,
boost::regex_match
仅在整个字符串都是匹配项时才找到匹配项; therefore, if there is a non-printable character somewhere, or maybe a trailing space character, that will perfectly explain the result you have seen. 因此,如果某个地方有不可打印的字符,或者尾部有空格的字符,则可以完美地解释您看到的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.