in reply to Regex help
Eventually someone will hit on the right technique; one that isn't plagued by lazy regexp engines, greedy matching, etc. But there's another possiblity...
You could make it easier on yourself, not worrying about trying to match ^(.*?) nongreedily, or about the lazy engine, or about (.*?)$ slurping everything up. Do it like this:
my $pattern = "AB"; print "pattern is $pattern\n"; my ( $middle ) = $string =~ /($pattern+)/; my ( $start, $end ) = ( $`, $' ); #.... and so on....
You take a performance hit in all regexp's in the program for using $` and $', but as I understand it, introducing capturing parens also introduces a similar performance hit for the current regular expression. And in non-time-critical operations (anything outside of tight loops) you don't really need to worry about the performance anyway right? ...so just do it the easy way.
If it turns out that you can't live with the speed-efficiency hit taken by leaning toward programming-efficiency, you can dig into other solutions. But the fact is that $`, $', and $& are there to be used, as long as you understand the ramifications of their use. To my knowledge, their use isn't deprecated, and it would seem that newer releases of Perl have even taken steps to make the use of those special variables more speed-efficiency friendly.
When the solution becomes so tricky that a dozen followup posts are still debating how to accomplish it, I think it's time to implement Perl's credo: There is more than one way to do it. (Start looking for a simpler solution). To that end, give my example a try.
Hope this helps...
Dave
"If I had my life to do over again, I'd be a plumber." -- Albert Einstein
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Regex help
by bart (Canon) on Aug 24, 2003 at 16:53 UTC | |
by davido (Cardinal) on Aug 24, 2003 at 17:09 UTC |