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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-08-2010, 09:28 AM
|
#1
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
regex.h question
Using the regex.h functions, how do you:
1) get the matched strings?
2) get the strings that matched sub-expressions?
|
|
|
|
06-08-2010, 11:53 AM
|
#2
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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;
}
|
|
|
|
06-08-2010, 03:43 PM
|
#3
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
Original Poster
|
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 03:46 PM.
|
|
|
|
06-08-2010, 04:09 PM
|
#4
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
Use Perl  ?
|
|
|
|
06-08-2010, 04:10 PM
|
#5
|
|
Senior Member
Registered: May 2005
Posts: 4,397
|
Quote:
Originally Posted by MTK358
...
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 ?
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:07 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|