Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Re: Regular expressions (multiple match problem)
On 2 huhti, 11:01, Antoninus Twink <nos...@nospam.invalid> wrote:
> On  2 Apr 2008 at  6:20, mikko.n wrote:
>
>
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>
> The problem is that you misunderstand what a match is.
>
> If the regex matches, then pm[0] contains the offsets of the (first)
> match for the whole regex. But pm[1],... don't contain the offets for
> subsequent matches of the whole regex, but rather contain the offsets of
> any parenthesized subexpressions that matched (in the match recorded in
> pm[0]).
>
> For example, try:
>
> #include <stdio.h>
> #include <regex.h>
> #include <sys/types.h>
>
> int main(void)
> {
>   regex_t p;
>   regmatch_t pm[2];
>   regcomp(&p,"k\\(.\\)",0);
>   regexec(&p,"mikko",2,pm,0);
>   printf("start=%d end=%d\n",pm[0].rm_so,pm[0].rm_eo);
>   printf("start=%d end=%d\n",pm[1].rm_so,pm[1].rm_eo);
>   regfree(&p);
>   return 0;
>
> }
>
> $ ./a
> start=2 end=4
> start=3 end=4

Is there then a simple alternative which would work so that it returns
all the matches of the original regexp in the text?

Mikko Nummelin

Report this thread to moderator Post Follow-up to this message
Old Post
mikko.n
04-02-08 09:45 AM


Re: Regular expressions (multiple match problem)
mikko.n wrote, On 02/04/08 09:37:
> On 2 huhti, 11:01, Antoninus Twink <nos...@nospam.invalid> wrote: 

<snip>

> Is there then a simple alternative which would work so that it returns
> all the matches of the original regexp in the text?

As Walter suggested, ask in a GNU group or mailing list where your
question would be topical (there is one specifically for regexp) instead
of comp.lang.c where it is not.

I note that this time you have added a cross post to
comp.unix.programmer where your question might be topical, but why
continue posting where it is not?
--
Flash Gordon

Report this thread to moderator Post Follow-up to this message
Old Post
Flash Gordon
04-03-08 12:42 AM


Re: Regular expressions (multiple match problem)
On  2 Apr 2008 at  8:37, mikko.n wrote:
> Is there then a simple alternative which would work so that it returns
> all the matches of the original regexp in the text?

Just use a loop, like this:


#include <stdio.h>
#include <regex.h>
#include <sys/types.h>

int main(void)
{
regex_t p;
regmatch_t pm;
char *s="mikko mikko";
regoff_t last_match=0;
regcomp(&p, "k", 0);
while(regexec(&p, s+last_match, 1, &pm, 0) == 0) {
printf("start=%d end=%d\n", pm.rm_so + last_match, pm.rm_eo + last_match);
last_match += pm.rm_so+1;
}
regfree(&p);
return 0;
}


Report this thread to moderator Post Follow-up to this message
Old Post
Antoninus Twink
04-03-08 12:42 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:31 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.