Posix regexec - matching subexpression

G

Gregory Toomey

Apologies if this is somehat offtopic.

Can somebody point to a simple example of matching subexpressions? I've read
http://www.gnu.org/software/libc/manual/html_node/Matching-POSIX-Regexps.html

Using the code below I can match "product_(.*)htm" to "product_abcd.htm".
What I get returned is "product_abcd.htm" but I want "abcd" (like the $1
matching variable in Perl). Is there an API for this?

gtoomey

------------
Return first substring mating the regex:

char *matchre ( char *pattern, char *string) {
char *p;
char *match;
int retval;
regex_t re;
char buf[BUFSIZE];
regmatch_t pmatch[100];
int status;
char *ps;
int eflag;

setlocale(LC_ALL, "");
if((status = regcomp( &re, pattern, REG_EXTENDED))!= 0){
regerror(status, &re, buf, 120);
exit(2);
}

ps = string;
eflag = 0;

match=(char *)malloc(1000);
if (status = regexec( &re, ps, 1, pmatch, eflag)==0){

/*printf("match found at: %d-%d, string=%s\n",
pmatch[0].rm_so, pmatch[0].rm_eo, ps
+pmatch[0].rm_so);*/
match=(char *)malloc(1000);
strcpy(match,ps +pmatch[0].rm_so);
/* return first match */
return match;
}
return "";
}
 
K

Keith Thompson

Gregory Toomey said:
Apologies if this is somehat offtopic.

It is. Standard C has no built-in support for regular expressions.
Try comp.unix.programmer.

[...]
match=(char *)malloc(1000);

The general consensus here is that casting the result of malloc() is a
bad idea. The cast is unnecessary, and it can sometimes (depending on
the compiler and settings) mask the error of forgetting the
"#include <stdlib.h>".

(If a troll with initials ERT posts a followup claiming that the cast
is necessary, ignore him.)
 
J

Jonathan Burd

The general consensus here is that casting the result of malloc() is a
bad idea. The cast is unnecessary, and it can sometimes (depending on
the compiler and settings) mask the error of forgetting the
"#include <stdlib.h>".

(If a troll with initials ERT posts a followup claiming that the cast
is necessary, ignore him.)

Of course, casting the result of malloc is a bad idea when using
standard C. However, most GNU code uses casts to make code compilable on
pre-ANSI compilers. I'm sure you know about it, but for the
benefit of other readers here who want to know more about it, here
is a link to Richard Heathfield's article on casting.

http://www.cognitiveprocess.com/~rjh/prg/writings/casting.html

That article says that one shouldn't cast malloc and
also gives a proper explanation for cases when one may want to.

Regards,
Jonathan.
 
M

Mark McIntyre

Of course, casting the result of malloc is a bad idea when using
standard C. However, most GNU code uses casts to make code compilable on
pre-ANSI compilers.

Its worth noting that while its laudable to continue to support compilers
that are more than 15 years old, there's no particularly food reason for
doing so and absolutely no good reason for learning this way.
 
R

Richard Bos

Jonathan Burd said:
Of course, casting the result of malloc is a bad idea when using
standard C. However, most GNU code uses casts to make code compilable on
pre-ANSI compilers.

You surprise me. I thought most GNU code was intentionally written to be
compilable on Ganuck, and Ganuck only. Other C compilers need not
comply; pre-ANSI compilers are laughed away.

Richard
 
B

Ben Pfaff

I thought most GNU code was intentionally written to be
compilable on Ganuck, and Ganuck only.

Most GNU programs compile on a wide range of C implementations.
 
R

Richard Bos

Ben Pfaff said:
Most GNU programs compile on a wide range of C implementations.

Do the gcc team know this? They must be tearing their hair out.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top