我的最后一个正则表达式不起作用,但我无法弄清楚原因

发布于 2025-01-07 06:09:41 字数 1591 浏览 0 评论 0原文

我有两个向量,一个保存我的正则表达式,另一个保存将根据正则表达式检查其中的字符串,其中大多数都工作正常,除了这个(如下所示)该字符串是正确的字符串并且与正则匹配表达式,但它输出不正确而不是正确。

输入字符串

.C/IATA

代码如下

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"

splitMask[split_id] = "[a-zA-Z] {1,15}" <---

但它仍然输出公司名称格式不正确

我不明白为什么它在应该正确的情况下打印错误,有人可以在这里帮助我吗?

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"

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洋洋洒洒 2025-01-14 06:09:41

您的正则表达式和周围的逻辑没问题。

您需要扩展日志记录并在调用 boost::regex_match 之前打印 splitMaskst 的相关部分,以仔细检查价值观就是你所相信的。打印它们并用一些标点符号包围,并打印字符串长度以确保确定。

您可能知道,boost::regex_match 仅在整个字符串匹配时才找到匹配项;因此,如果某处有不可打印的字符,或者可能是尾随空格字符,这将完美地解释您所看到的结果。

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. 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; therefore, if there is a non-printable character somewhere, or maybe a trailing space character, that will perfectly explain the result you have seen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文