472,989 Members | 3,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 2846
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John | last post by:
Internet Facing SQL Server, Login Security? How? I would think that our Project is fairly common. We're stuck on a security question. We have a typical SQL Server 2000, running under Windows...
4
by: sujeetkuk | last post by:
I am trying to compile perl source (5.8.7) using GCC .But after the .Configure step as I run make command , I get a error that miniperl-gcc-3.3.2': No such file or directory" or it says gcc -b option...
0
by: Mudassar | last post by:
I am facing a problem while accessing the gridview in nested gridview structure. Some time i found child gridview control but when i bind data to child gridview it doesnt
2
by: rakesh kumawat | last post by:
I am facing a problem while reading the result which is loaded in DOMDocument. In which I am sending a request to web service and getting a record of Single Order. This is my VB Code which is i am...
2
by: alzforu | last post by:
The code in which i m facing the problem is: //file stream.cc #include <vector> #include <algorithm> #include <iostream> using namespace std;
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 =...
0
by: Niks | last post by:
I am facing a very strange problem in my websevice which is working on socket communication. While i am publishing my webservice to the IIS and invoking a method, a data loss problem in occuring....
4
by: =?Utf-8?B?U3VoYXMgVmVuZ2lsYXQ=?= | last post by:
Hello, I am facing an issue while querying Active directory using C# code with system.DirectoryServices namespace. Here is the path for my LDAP - "LDAP://CN=XY - C++/Unix and other,...
4
by: Man4ish | last post by:
I am trying to use regex3 lib for parsing the file in C using following program. #include <sys/types.h> #include <regex.h> #include <stdio.h> int main(int argc, char **argv) { int r; ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.