我尝试过不同的函数,包括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>.更多推荐
发布评论