C试图匹配确切的子字符串,仅此而已(C Trying to match the exact substring and nothing more)

编程入门 行业动态 更新时间:2024-10-19 22:29:50
C试图匹配确切的子字符串,仅此而已(C Trying to match the exact substring and nothing more)

我尝试过不同的函数,包括strtok() , strcmp()和strstr() ,但我想我错过了一些东西。 有没有一种方法来匹配字符串中的确切子字符串?

例如:

如果我有一个名字: “Tan”

我有2个文件名:“SomethingTan5346”和“nothingTangyrs634”

那么如何确保我匹配第一个字符串,而不是两个? 因为第二个文件是为人Tangyrs。 还是这种方法不可能? 我是否以错误的方式去做?

I have tried different functions including strtok(), strcmp() and strstr(), but I guess I'm missing something. Is there a way to match the exact substring in a string?

For example:

If I have a name: "Tan"

And I have 2 file names: "SomethingTan5346" and "nothingTangyrs634"

So how can I make sure that I match the first string and not both? Because the second file is for the person Tangyrs. Or is it impossible with this approach? Am I going at it the wrong way?

最满意答案

解决方案取决于您对精确匹配的定义。 这可能对你有用:

遍历目标子字符串的所有匹配项。

C查找所有出现的子串

在字符串中查找子字符串的所有实例

找到字符串中的子字符串的计数

https://cboard.cprogramming.com/c-programming/73365-how-use-strstr-find-all-occurrences-substring-string-not-only-first.html

等等

确定匹配的范围后,确认前面和后面的字符匹配/不符合“完全匹配”的标准。

要么,

你可以利用C ++中的正则表达式(我知道标签是“C”),包含#include <regex>或POSIX #include <regex.h> 。

The solution depends on your definition of exact matching. This might be useful for you:

Traverse all matches of the target substring.

C find all occurrences of substring

Finding all instances of a substring in a string

find the count of substring in string

https://cboard.cprogramming.com/c-programming/73365-how-use-strstr-find-all-occurrences-substring-string-not-only-first.html

etc.

Having the span of the match, verify that the previous and following characters match/do not match your criterion for "exact match".

Or,

You could take advantage of regex in C++ (I know the tag is "C"), with #include <regex>, or POSIX #include <regex.h>.

更多推荐

本文发布于:2023-04-29 12:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336336.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:仅此而已   字符串   确切   substring   exact

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!