问题
Can I use the strstr function to match exact word? For example, let's say I have the word hello
, and an input string line
:
if
char* line = "hellodarkness my old friend";
and I use
result = strstr(line, "hello");
result
will match (be not NULL), however I want to match only the exact word "hello" (so that "hellodarkness" would not match) and result will be NULL.
Is it possible to do this using strstr
or do I have to use fscan
and scan the line word by word and check for matches?
回答1:
I would:
- check if string is in sentence
- if found at start (same pointer as
line
), add the length of the word and check if alphanumerical char found. If not (or null-terminated), then match - if found anywhere else, add the extra "no alphanum before" test
code:
#include <stdio.h>
#include <strings.h>
#include <ctype.h>
int main()
{
const char* line = "hellodarkness my old friend";
const char *word_to_find = "hello";
char* p = strstr(line,word_to_find);
if ((p==line) || (p!=NULL && !isalnum((unsigned char)p[-1])))
{
p += strlen(word_to_find);
if (!isalnum((unsigned char)*p))
{
printf("Match\n");
}
}
return 0;
}
here it doesn't print anything, but insert a punctuation/space before/after or terminate the string after "hello"
and you'll get a match. Also, you won't get a match by inserting alphanum chars before hello.
EDIT: the above code is nice when there's only 1 "hello"
but fails to find the second "hello"
in "hellohello hello"
. So we have to insert a loop to look for the word or NULL
, advancing p
each time, like this:
#include <stdio.h>
#include <strings.h>
#include <ctype.h>
int main()
{
const char* line = " hellohello hello darkness my old friend";
const char *word_to_find = "hello";
const char* p = line;
for(;;)
{
p = strstr(p,word_to_find);
if (p == NULL) break;
if ((p==line) || !isalnum((unsigned char)p[-1]))
{
p += strlen(word_to_find);
if (!isalnum((unsigned char)*p))
{
printf("Match\n");
break; // found, quit
}
}
// substring was found, but no word match, move by 1 char and retry
p+=1;
}
return 0;
}
回答2:
Here is a generic function for your purpose. It returns a pointer to the first match or NULL
if none can be found:
#include <ctype.h>
#include <string.h>
char *word_find(const char *str, const char *word) {
const char *p = NULL;
size_t len = strlen(word);
if (len > 0) {
for (p = str; (p = strstr(p, word)) != NULL; p++) {
if (p == str || !isalnum((unsigned char)p[-1])) {
if (!isalnum((unsigned char)p[len]))
break; /* we have a match! */
p += len; /* next match is at least len+1 bytes away */
}
}
}
return p;
}
回答3:
Since strstr()
returns the pointer to the starting location of the substring that you want to identify, then you can use strlen(result)
the check if it is a substring of longer string or the isolated string that you are looking for. if strlen(result) == strlen("hello")
, then it ends correctly. If it ends with a space or punctuation (or some other delimiter), then it is also isolated at the end. You would also need to check if the start of the substring is at the beginning of the "long string" or preceded by a blank, punctuation, or other delimiter.
来源:https://stackoverflow.com/questions/42352846/matching-an-exact-word-using-in-c