LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 02-07-2010, 02:44 AM   #1
bellhuang
LQ Newbie
 
Registered: Feb 2010
Location: China
Posts: 18

Rep: Reputation: 0
Could anyone show me a simple example of regex or pcre program on linux C?


I am trying to use regex or pcre to find out the matched from multiple textfiles. Could anyone share me a example of how to write a relative program on linux C? Thanks in advance.
 
Old 02-07-2010, 03:56 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,364
Blog Entries: 52

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
pcre comes with a demo program.
 
Old 02-07-2010, 06:44 AM   #3
bellhuang
LQ Newbie
 
Registered: Feb 2010
Location: China
Posts: 18

Original Poster
Rep: Reputation: 0
why following program can't work?
#include<stdio>
#include<sys/types.h>
#include<regex.h>

int main()
{**int status;
char str[]="abcd";
char pattern[]="a.";
* **regex_t *preg;
* **regcomp(preg, pattern, REG_EXTENDED|REG_NOSUB)
status=regexec(preg,str,1,NULL,0);
* * while(status==0) printf("ok");
regfree(preg);
}
 
Old 02-07-2010, 07:05 AM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,364
Blog Entries: 52

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
It crashes because the program does not allocate space for the pattern storage area that preg points to. With this allocated, it will work:

Code:
#include <stdio.h>
#include <regex.h>
int main()
{
    int status;
    char str[]="abcd";
    char pattern[]="a.";
    regex_t reg;
    regcomp(&reg, pattern, REG_EXTENDED|REG_NOSUB);
    status=regexec(&reg,str,1,NULL,0);
    if (status==0)
        printf("match\n");
    else
        printf("no match\n");
    regfree(&reg);
}

Last edited by neonsignal; 02-07-2010 at 07:08 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Simple regex match without replace? nrg Programming 1 11-21-2009 04:04 AM
LXer: RegEx Buddy--A Simple Way to Create Regular Expressions LXer Syndicated Linux News 0 02-15-2009 03:01 AM
PCRE Regex Osiris990 Programming 10 10-22-2007 03:43 PM
simple regex not so simple (perl) ludeKing Programming 5 03-02-2005 03:29 AM
simple perl and regex phlx Programming 6 12-03-2004 04:01 PM


All times are GMT -5. The time now is 06:08 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration