strstr 和要匹配的出现次数

发布于 2024-08-30 16:32:34 字数 461 浏览 5 评论 0原文

我有几个可能的情况需要使用 strstr 进行测试。

if ((a = strstr(string, "FOO")) != NULL || (a = strstr(string, "BAR")) != NULL ||
    (a = strstr(string, "FOO2")) != NULL ||(a = strstr(string, "BAR2")) != NULL ||
    (a = strstr(string, "FOO3")) != NULL ||(a = strstr(string, "BAR3")) != NULL) // do something

然后根据发现我需要做的事情,

 var = strlen("THE_ONE_MATCHED_ABOVE");

在不使用大量 if 语句的情况下执行此操作的好方法是什么?

I have several possible occurrences to test with strstr.

if ((a = strstr(string, "FOO")) != NULL || (a = strstr(string, "BAR")) != NULL ||
    (a = strstr(string, "FOO2")) != NULL ||(a = strstr(string, "BAR2")) != NULL ||
    (a = strstr(string, "FOO3")) != NULL ||(a = strstr(string, "BAR3")) != NULL) // do something

and then based on the occurrence found I need to do

 var = strlen("THE_ONE_MATCHED_ABOVE");

What would be a good way to do this without using lots of if statements?

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

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

发布评论

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

评论(6

绮烟 2024-09-06 16:32:34

您是否愿意使用逗号运算符:

if ((lookfor = "FOO", a = strstr(string, lookfor)) != NULL ||
    (lookfor = "BAR", a = strstr(string, lookfor)) != NULL ||
    ...)
{
    var = strlen(lookfor);
}

逗号运算符将允许您按从左到右的顺序计算多个表达式。整个表达式的值是最右边的 esub 表达式的值。

Are you willing to use the comma operator:

if ((lookfor = "FOO", a = strstr(string, lookfor)) != NULL ||
    (lookfor = "BAR", a = strstr(string, lookfor)) != NULL ||
    ...)
{
    var = strlen(lookfor);
}

The comma operator will allow you to evaluate multiple expression in left to right order. The value of the expression as a whole is the value of the rightmost esub-expression.

守望孤独 2024-09-06 16:32:34

我觉得你的例子过于简单化了,但值得注意的是它可以简化。如果未找到字符串 "FOO",那么您就知道不会找到字符串 "FOO2",因此您可以消除除前两种情况之外的所有情况例子。

I have a feeling that your example is oversimplified, but it's probably worth noting that it can be simplified. If the string "FOO" isn't found, then you know the string "FOO2" won't be found so you can eliminate all but the first two cases in your example.

森林散布 2024-09-06 16:32:34
int match_length( const char* string) {
/* return 0 if no match, otherwise strlen of match */

   static const char* const MATCHES[] = { "BAR2", "FOO2", "FOO3", "FOO", "BAR", "..." } ;
   // NB: MATCHES must be sorted in descending order of length (longest first).
   const char* r = 0;
   int i = 0 ;

   for( ; 
        i < sizeof(MATCHES) / sizeof(MATCHES[0]) 
        && ( r = strstr( string, MATCHES[i] ) ) == 0; 
        ++i );

   return r ? strlen( MATCHES[i] ) : 0 ;
}

注意:caf 提出了一个非常重要的观点:“您需要按长度降序排列匹配 - 如果 strstr(x, "FOO2") 为非空,那么 strstr(x, "FOO") 也为非空,因此您需要先找到前者。”我已编辑以反映这一点。根据此函数的使用,排序也可能在运行时完成。

int match_length( const char* string) {
/* return 0 if no match, otherwise strlen of match */

   static const char* const MATCHES[] = { "BAR2", "FOO2", "FOO3", "FOO", "BAR", "..." } ;
   // NB: MATCHES must be sorted in descending order of length (longest first).
   const char* r = 0;
   int i = 0 ;

   for( ; 
        i < sizeof(MATCHES) / sizeof(MATCHES[0]) 
        && ( r = strstr( string, MATCHES[i] ) ) == 0; 
        ++i );

   return r ? strlen( MATCHES[i] ) : 0 ;
}

Note: caf makes a very important point: "You need to arrange the MATCHES in descending length order - if strstr(x, "FOO2") is non-null, then so is strstr(x, "FOO"), so you need to find the former first." I've editted to reflect this. Depending on the use of this function, the sort might also be done at runtime.

可遇━不可求 2024-09-06 16:32:34

如果您要在主题字符串中查找很多模式,最好的选择是 Aho-Corasick算法

If you have a lot of patterns to look for in subject string, the best choice is Aho-Corasick algorithm:

情泪▽动烟 2024-09-06 16:32:34

如果您有大量不同的字符串需要以这种方式进行搜索,您可能应该使用正则表达式库并编译 DFA 来匹配 (FOO|FOO2|FOO3|...)。这是最优解。您还可以自己计算出树结构来实现相同的结果,特别是如果要搜索的字符串是常量并且您可以有效地对它们进行硬编码。

If you have a large varying set of strings you need to search for this way, you should probably use a regular expression library and compile a DFA to match (FOO|FOO2|FOO3|...). This is the optimal solution. You could also work out the tree structures to implement the same result yourself, especially if the strings to search for are constant and you can hard-code them efficiently.

剑心龙吟 2024-09-06 16:32:34

如果您在代码中经常使用该模式,则应该编写一个如下所示的小辅助函数:

int FindFirstMatch(const char* stringItem, const char** stringList)
{
    int index = -1;
    int itemLength = strlen(stringItem);

    if (stringList == NULL)
    {
        return index;
    }

    for (; stringList[index] != NULL; index++)
    {
            if (  (strlen(stringList[index]) == itemLength)
               && (strstr(stringList[index], stringItem) != NULL))
        {
            break;
        }
    }
    return index;
}

该函数采用一个字符串和一个以 NULL 结尾的字符串数组作为参数,并返回第一次出现的索引列表中的字符串。然后您可以使用索引来检查字符串长度。

要像在示例中那样进行检查,您可以编写:

const char* itemList[] = {"FOO", "FOO2", "FOO3", "BAR", "BAR2", "BAR3", NULL};
int itemLength = 0;

int index = FindFirstMatch("BAR3", itemList);
if (index != -1)
{
    itemLength = strlen(itemList[index]);
}

If you use that pattern frequently within your code, you should write a small helper function like this:

int FindFirstMatch(const char* stringItem, const char** stringList)
{
    int index = -1;
    int itemLength = strlen(stringItem);

    if (stringList == NULL)
    {
        return index;
    }

    for (; stringList[index] != NULL; index++)
    {
            if (  (strlen(stringList[index]) == itemLength)
               && (strstr(stringList[index], stringItem) != NULL))
        {
            break;
        }
    }
    return index;
}

The function takes a string and a NULL terminated string array as arguments and returns the index of the first occurence of the string in the list. You can then use the index to check fo the string length.

To do a check as you did in your example you would instead write:

const char* itemList[] = {"FOO", "FOO2", "FOO3", "BAR", "BAR2", "BAR3", NULL};
int itemLength = 0;

int index = FindFirstMatch("BAR3", itemList);
if (index != -1)
{
    itemLength = strlen(itemList[index]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文