473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Posix regexec - matching subexpression

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/man...X-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 "";
}
Nov 14 '05 #1
6 6214
Gregory Toomey <no****@bigpond.com> writes:
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #2
Keith Thompson wrote:
Gregory Toomey <no****@bigpond.com> writes:

<snip>

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...s/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.

--
Email: "jonathan [period] burd [commercial-at] gmail [period] com" sans-WSP

"I usually swear at C++, but so far it has never sworn back."
- Ben "Noir"
Nov 14 '05 #3
On Sat, 19 Feb 2005 15:51:31 +0530, in comp.lang.c , Jonathan Burd
<jb***@nospam.com> wrote:
Keith Thompson wrote:

The general consensus here is that casting the result of malloc() is a
bad idea.


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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #4
Jonathan Burd <jb***@nospam.com> wrote:
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
Nov 14 '05 #5
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
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.
--
"I should killfile you where you stand, worthless human." --Kaz
Nov 14 '05 #6
Ben Pfaff <bl*@cs.stanford.edu> wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
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.


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

Richard
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Robert Kern | last post by:
I'm in the middle of writing a scientific program with a number of very, very long formulae. You can see a typical one below. What I would like to do is to parse this code to yield an AST, identify...
8
by: Christian Gudrian | last post by:
Hello, there! Given a list with strings. What is the most pythonic way to check if a given string starts with one of the strings in that list? I started by composing a regular expression...
5
by: jens | last post by:
I was wondering if anyone could tell be whether it's possible to instruct preg_match not to save subexpressions matches under 0, 1...n keys when using named subexpressions; I.e instruct preg_named...
5
by: Nick Barr | last post by:
Hi, Just noticed something funny with the POSIX Regular expressions and wondered if it is a bug, or whether I am doing something wrong. I am using the POSIX RE in a function that is used in...
2
by: zoltan | last post by:
Hi, I am using a function called regexec() as defined in the regex.h header in libc.a. I am coding on Solaris 8 machine. The function is defined as follows : int regexec(const regex_t * preg,...
16
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
2
by: apoorva.groups | last post by:
Hi I am facing problem while using regexec function. Ex: String = "abc_def_hig" sub string = "def" regexc if I use regexec the it will find the sub string in string and it will return 0....
1
by: al.moorthi | last post by:
the below program is working in Suse and not working on Cent 5: can any body have the solution ? #include <regex.h> #include <stdlib.h> #include <stdio.h> int main(){ char cool =...
23
by: asit | last post by:
what is the difference between portable C, posix C and windows C ???
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.