facing problem while using regexec

A

apoorva.groups

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
 
M

Mark Bluemel

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);
}
 
B

Ben Bacarisse

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.

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

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top