MLUG: Re: [MLUG] String manipulation in C
Re: [MLUG] String manipulation in C
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
I think I have it figured out. I did so by calling my match() below to tell me if there is a match (action = "" If there was a match, I got the starting and ending locations from match() with action = 1 and 2. I set up a cumulative distance counter of pmatch[].rm_eo values returned and then advanced the string to be searched by that many characters. I put that in a while loop to keep advancing the string and looking for matches until there were no more:

/* Sending the match function a string and pattern, getting a status result */
matchStatus = match(sequence, pattern, 0);

/* Outputting data */
printf("%-15d%-15s%-80s", num, names, pattern);

/* Actions taken if there is a match
/* Getting the starting and ending locations of a match (if there is one) */
while(matchStatus == 1) {
/* Advancing string past the end location of the previous match.
    Note that this will just copy the string for the first match. */
strcpy(stringrem, sequence+endLocTotal);

/* Getting the relative starting and ending locations of the matches */
startLoc = match(stringrem, pattern, 1);
endLoc = match(stringrem, pattern, 2);

/* Getting the cumulative distance of the matches from the beginning and end of the sequence */
startLocTotal = startLocTotal + startLoc;
endLocTotal = endLocTotal + endLoc;

/* Printing the values of the starting and ending locations if there was a new match */
if(startLoc > 0) {
printf("%-7d%-7d", startLocTotal, endLocTotal);
}

/* pass sequence after the first match to match() to see if there is another match */
matchStatus = match(stringrem, pattern, 0);

/* Flushing values of StartLoc and endLoc */
startLoc = 0;
endLoc = 0;
}

Here's my match function:

int match(const char *string, char *pattern, int action) {

/* Declaring function variables */
int status;
int i = 1;
regex_t re;
regmatch_t pmatch[10];

/* Compile the regular _expression_ and exit if there is an error with it */
if(regcomp(&re, pattern,0) != 0) {
return 0;
}

/* Perform the regular _expression_ matching function */
status = regexec(&re, string, 10, pmatch, 0);

/* Exit and retun 0 if no match is found */
if(status != 0) {
return 0;
}

/* Returning different results depending on what's being asked of the function */
/* action = 0 tells the function to return if there is a match or not */
if(action == 0) {
if(status == 0) {
return 1;
}
if(status !=0 ) {
return 0;
}
}

/* action = 1 tells the function to retun the location where the match starts */
if(action == 1 && status == 0) {
return pmatch[0].rm_so;
}

/* action = 2 tells the function to return the location where the match ends */
if(action == 2 && status == 0) {
return pmatch[0].rm_eo;
}

/* Freeing the memory for pointer to the string */
regfree(&re);
}

It appears to do just the job that I want and is much faster than MATLAB. Thank you all for your help.

Jack
_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members