473,395 Members | 1,504 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,395 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 2878
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.