I'm trying to learn some C in order to make some changes to some existing programs that are written in C. For what it's worth all my experience to date has been with PHP, so this is quite different for me.

Basically, I want to have an array, (patterns in my example), that has a regex pattern, and then two strings that I will eventually insert before and after the matched pattern. (I'm not there yet, still working on the matching...)

Then I want to loop through each pattern for a given string (tmp_char in this example)

Here's my code:

#include <stdio.h>
#include <regex.h>
#include <strings.h>

#define MAXMATCH 3 

int main()
	struct {
		const char *pattern;
		const char *insert_before;
		const char *insert_after;
		regex_t compiled;
	} patterns [] = {
		{ "the", "TB", "TA"},
		{ "cat", "CB", "CA"},
		{ "dog", "DB", "DA"}

	int i;
	int c;
	int result;
	regmatch_t matches[MAXMATCH];
	char tmp_char[4096] = "the bad dog chased the big cat around ";

	/* compile regexs and store the compiled form in the array */

	for (i = 0; i < sizeof(patterns)/sizeof(patterns[0]); i++)
		if (regcomp(&patterns[i].compiled, patterns[i].pattern, REG_EXTENDED))	
			fprintf(stderr, "Error compiling pattern: %s\n",
			return 1;

	/* execute regexs */
        for (i = 0; i < sizeof(patterns)/sizeof(patterns[0]); i++)
		result = regexec(&patterns[i].compiled, tmp_char, MAXMATCH, matches, 0);
		printf("result for %s : %d\n",patterns[i].pattern, result);
		if (result == 0) /* we matched! */
			for (c = 0; c < MAXMATCH; c++) 
				printf("rm_so: %d\nrm_eo: %d\n\n", (int)matches[i].rm_so,



The big issue issue I'm having with this code:

Only the first time through the loop works. The results of the matching for the second two patterns (variables rm_so and rm_oe) is always -1. It does match the regex though, so I think the compiling of the regex works...

Also, can someone explain the regmatch_t structure? It's an array, which leads me to assume that if a pattern is matched more than once in the string, it will populate the array with each occurance of the match, but that doesn't seem to be the case. (there are more than one "the"'s in the string, but only the first is returned)

Thanks in advance!

5 Years
Discussion Span
Last Post by mx tommy

At lines 49-50, why do you index matches with i?

OMG... You don't know how long I searched to find what was the problem! I thought maybe I was doing something wrong with memory or pointers (both being very new to me) Such a stupid mistake... Thanks a million for taking the time to look at the code and point out my noob error! :| I changed i for c and now it works like it's supposed to!

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.