468,510 Members | 1,619 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,510 developers. It's quick & easy.

facing problem while using regexec

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.
I want to modify the sub string such that it matches only if the
string "starts" with the sub string.

if String = "def_abc_hig and sub_String = "def"
then only regexec should return 0
else String is "abcdefhig" and sub_String = "def"
the regexec should fail

I tried using ^ in from of the sub string but then it does not event
match the String.

here is the code snippet.
************************************************** ***************************
int regexpress ( char *string, char *pattern )
{
regex_t * regex;
printf("\nInside regex\n");
regex = ( regex_t *) malloc (sizeof(regex_t));
if(regcomp(regex, pattern, REG_EXTENDED)!=0) {
printf("\nERROR\n");
return -1;
}
if (regexec(regex, string, 0, NULL, 0)==0) {
printf("\nInside regexpress pattern found\n");
regfree(regex);
free(regex);
return 1;
}
else {
printf("\nInside else\n");
regfree(regex);
free(regex);
return -22;
}

}
retCode = regexpress( "abcdefhig", "def");
if (retCode == 1){
printf("\npattern found file found\n");
return 1;
}

retCode = regexpress( "defabchig", "def");
if (retCode == 1){
printf("\npattern found file found\n");
return 1;
}

************************************************** ***********************************

I want that the Ist regexpress function to return 0 and 2nd regexpress
function to return 1
What Pattern should i be sending

Thanks In Advance
Dec 7 '07 #1
2 2574
ap************@gmail.com wrote:
Hi

I am facing problem while using regexec function.
First point - there is no regexec function in Standard C. Many people
would consider that you should ask your question in a group which
discusses contexts where regexec exists.

As regexec is part of POSIX, comp.unix.programmer seems a reasonable
choice. (If my newsreader allowed me to set followups I would...)
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.
Second point. Even if you go to a more appropriate group, you will do
better giving decent data. A small self-contained testcase is vastly
better than a few snippets and a vague discussion.

Put the effort in to reduce your program to the smallest version which
illustrates the problem. Often by doing this, you'll solve the problem
yourself. If you still can't fix it, cut and paste the sample into your
posting, so that anyone else can see _exactly_ what the code is and
does.

As I happened to have a suitable system to hand, I rapidly produced a
testcase which seemed to do what I expected from my manual pages. So
you need to produce a much better explanation of your problem.

When you've done so, please pursue this in a different newsgroup, not
comp.lang.c.

Here for reference is what I tried :-

#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
void is_matched(char *string, char *pattern) {
regex_t *reg = malloc(sizeof *reg);
regcomp(reg,pattern,REG_EXTENDED); /* needs error handling!*/
if (regexec(reg,string,(size_t)0,NULL,0) != 0) {
printf("[%s] appears not to contain [%s]\n",string,pattern);
} else {
printf("[%s] appears to contain [%s]\n",string,pattern);
}
regfree(reg);
}

int main(void) {
char *strings[] = {"abcfedghi", "abcdefghi","defghijkl", NULL};
char *patterns[] = {"def", "^def", NULL};
char **nextString;
char **nextPattern = patterns;
for(nextPattern = patterns ; *nextPattern != NULL ; nextPattern++)
for(nextString = strings ; *nextString != NULL ; nextString++)
is_matched(*nextString,*nextPattern);
}
Dec 7 '07 #2
"ap************@gmail.com" <ap************@gmail.comwrites:
I am facing problem while using regexec function.
This is (probably) the POSIX regexec function, so I would suggest you
post in comp.unix.programmer. If you are using some other regexp
library, then you should find another group to post to.

<snip>
I want to modify the sub string such that it matches only if the
string "starts" with the sub string.
<snip>
I tried using ^ in from of the sub string but then it does not event
match the String.
The problem is not with the code you posted. When you post to
comp.unix.programmer, post a whole, compilable, example program with a
case that fails (adding ^ works for me with the code you posted).

--
Ben.
Dec 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by alzforu | last post: by
1 post views Thread by al.moorthi | last post: by
4 posts views Thread by =?Utf-8?B?U3VoYXMgVmVuZ2lsYXQ=?= | last post: by
4 posts views Thread by Man4ish | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.