LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-08-2010, 10:28 AM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
regex.h question


Using the regex.h functions, how do you:

1) get the matched strings?

2) get the strings that matched sub-expressions?
 
Old 06-08-2010, 12:53 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Try Googling for "C regex tutorial".

Here are a couple of hits:

http://cboard.cprogramming.com/c-pro...g-matches.html

http://wiki.hypexr.org/wikka.php?wakka=RegexExamples

http://www.peope.net/old/regex.html

Here's a complete example (no, I haven't actually tried it ):
Code:
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>

char *regexp (char *string, char *patrn, int *begin, int *end) {     
        int i, w=0, len;                  
        char *word = NULL;
        regex_t rgT;
        regmatch_t match;
        regcomp(&rgT,patrn,REG_EXTENDED);
        if ((regexec(&rgT,string,1,&match,0)) == 0) {
                *begin = (int)match.rm_so;
                *end = (int)match.rm_eo;
                len = *end-*begin;
                word=malloc(len+1);
                for (i=*begin; i<*end; i++) {
                        word[w] = string[i];
                        w++; }
                word[w]=0;
        }
        regfree(&rgT);
        return word;
}


int main() {
	int b,e;
	char *match=regexp("this and7 that","[a-z]+[0-9]",&b,&e);
	printf("->%s<-\n(b=%d e=%d)\n",match,b,e);
	return 0;
}
 
Old 06-08-2010, 04:43 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
I made a simple grep-like program and it works:

Code:
#include <stdio.h>
#include <regex.h>

int main(int argc, char **argv)
{
	if (argc != 2)
	{
		fprintf(stderr, "Usage: %s [ERE]\n", argv[0]);
		return 1;
	}
	
	regex_t re;
	regmatch_t match;
	int status;
	status = regcomp(&re, argv[1], REG_EXTENDED);
	
	if (status != 0)
	{
		fprintf(stderr, "error: %s: invalid ERE\n", argv[1]);
		return 2;
	}
	
	char line[512];
	while (fgets(line, 512, stdin))
	{
		status = regexec(&re, line, 1, &match, 0);
		if (status == 0)
		{
			printf("%d-%d: %s\n", match.rm_so, match.rm_eo, line);
		}
	}
	
	return 0;
}

// EOF
But how do I get the substrings matched by sub-expressions contained in "()"s?

And what about multiple matches in the same string, not just the first one?

Last edited by MTK358; 06-08-2010 at 04:46 PM.
 
Old 06-08-2010, 05:09 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Use Perl ?
 
Old 06-08-2010, 05:10 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by MTK358 View Post
...
But how do I get the substrings matched by sub-expressions contained in "()"s?

And what about multiple matches in the same string, not just the first one?

And why do you think

man 3 regex

does not provide the needed info ?
 
  


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
regex noob question : a(b|c) and a[bc] same ? hottdogg Programming 2 03-28-2008 11:04 AM
Regex question once again Isotonik Linux - Newbie 2 06-14-2006 03:15 PM
regex question Toadman Linux - General 0 12-30-2005 01:59 PM
sed / regex question whysyn Linux - General 3 06-28-2005 03:11 PM
perl regex question JustinHoMi Programming 5 03-20-2002 08:46 AM


All times are GMT -5. The time now is 03:15 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