regexp: saving backreferences that won't overwrite existing values 
Author Message
 regexp: saving backreferences that won't overwrite existing values

You could change how you group expressions

#!/usr/bin/perl
$_="test: begin foo bar bat baz end";
/begin(?:\s(\w+))*\send/;
print "$1\n";  # prints 'baz'
/begin((?:\s\w+)*)\send/;
print "$1\n";  # prints ' foo bar bat baz'

Quote:

> If I do something like

> /begin(?:\s(\w+))*\send/

> I'll have last word matched at $1. Any way to make them "pile" up so
> that I'll get everything that was at $1 before it got overwritten?

> I tried like the following:


> but it saved all the failed matches too. Of course, I guess this was
> because my construct had to be backtracked (engine matching "end" at
> first, then realizing it doesn't work), and I guess it would have worked
> with some proper zero-width assertions, but still the idea shouldn't
> change; that there should be a way to save everything the "(\w+)" _did_
> match in the end.

> How?

> P.S. I realize that there were other ways to do the example above; it's
> merely to demonstrate the feature I'd like in the regexp. To do in
> single regexp, you see? I'm sure I could've come up with better example
> that didn't solve with some splitting and/or loops, but ...

>  -Kaatunut



Tue, 29 Apr 2003 12:42:47 GMT  
 regexp: saving backreferences that won't overwrite existing values
You could change how you group expressions

#!/usr/bin/perl
$_="test: begin foo bar bat baz end";
/begin(?:\s(\w+))*\send/;
print "$1\n";  # prints 'baz'
/begin((?:\s\w+)*)\send/;
print "$1\n";  # prints ' foo bar bat baz'

Quote:

> If I do something like

> /begin(?:\s(\w+))*\send/

> I'll have last word matched at $1. Any way to make them "pile" up so
> that I'll get everything that was at $1 before it got overwritten?

> I tried like the following:


> but it saved all the failed matches too. Of course, I guess this was
> because my construct had to be backtracked (engine matching "end" at
> first, then realizing it doesn't work), and I guess it would have worked
> with some proper zero-width assertions, but still the idea shouldn't
> change; that there should be a way to save everything the "(\w+)" _did_
> match in the end.

> How?

> P.S. I realize that there were other ways to do the example above; it's
> merely to demonstrate the feature I'd like in the regexp. To do in
> single regexp, you see? I'm sure I could've come up with better example
> that didn't solve with some splitting and/or loops, but ...

>  -Kaatunut



Tue, 29 Apr 2003 12:43:14 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. CGI.pm: saving query values without the '%20's

2. Crypt-RSA won't save keys

3. RSA Key won't save

4. Regexp containing plus-chars won't work?!?!?

5. substitution variable won't change in regexp

6. Backreferences that create %% values?

7. mod_perl with CGI.pm won't always forget previous values

8. Message box won't return a value

9. Overwriting values in hash of hashes

10. couldn't get sysseek/syswrite combination to overwrite a line.e

11. UNIX to %$$@! Win: Script Won't Go

12. Perl 5 an GD - saving images - Win 32

 

 
Powered by phpBB® Forum Software