Email address obfuscation in effect -- please
click here to turn it off.
[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
On Sun, 1 Jul 2007, Stephen Montgomery-Smith wrote:
On Sun, 1 Jul 2007, Jack Smith wrote:
I have a little problem that you guys might be able to solve. I am
trying to find out where a short sequence of letters matches in a much
longer sequence of letters. I can find the location of the first match,
but I cannot make it find any subsequent matches and need to. After
reading the GNU regex documentation, I don't think that the regexec()
function searches beyond one match of pattern in string.
So what I'd like to do is hack everything off of the first string after
the end of the first match. If I am looking for "ABC" in the following
string:
XXXXXABCXXXXXABCXX
I'd like to reduce the string to
XXXXXABCXX
after the first "ABC" is found so that the second match can be found. I
cannot find a function that will copy the last n digits of a string into
an array (sort of an opposite to strncpy()) so that I could simply
cleave out anything before and including the match in string, then
search the remainder.
Any help would be appreciated.
This is actually much easier than you think. If the string s (which is
actually a pointer) contains
"abcdefgh"
then the string s+4 contains
"efgh"
So just put s+4 into your search function instead of s. No copying needed
(although if you wanted to copy it, just put s+4 into strncpy or strcpy or
whatever).
Also you might want to look at the function strstr instead of regexec.
For what you are wanting to do, it looks a lot simpler. So suppose you
are looking for the third occurence of "ABC" in "XXABCXXABCXXABCXXX":
char *s, *t, *u;
strcpy(s,"ABC");
strcpy(t,"XXABCXXABCXXABCXXX");
u = strstr(t,s);
if (u!=NULL) u = strstr(u,s);
if (u!=NULL) u = strstr(u,s);
if (u!=NULL) printf("%d\n",u-s); else printf("Not found");
_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members