Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: OS X vs. My Regex

by tall_man (Parson)
on Apr 16, 2003 at 19:04 UTC ( #251003=note: print w/replies, xml ) Need Help??


in reply to OS X vs. My Regex

You are making the regular expression engine do a lot of extra work by:

1) Using capturing parenthesis instead of noncapturing ones .
2) Even a non-greedy dot-star is bad (see Death to Dot Star!). Use a negated character class like [^\]]* instead.
3) Excessive going in and out from parenthesis levels, as in the [^\[] expression following the "|", which ought to have a "+" after it.

These inefficiencies are probably causing the regular expression engine to go wild and run out of memory. By the way, why not use module Text:Balanced instead?

Update: Here is a brushed-up version with my suggestions added:

if ($text =~ /^ (?: # Any amount of: (?: \[ # An open brace [^\]]* # Any amount of non-brace stuff \] # A close brace ) | # Or [^\[]+ # Anything that's not an open brac +e. )* \] # Followed by a close brace /xs

Update2: After recommending Text::Balanced for this problem, I decided to try it for myself. I would have expected the following code to work, but it doesn't (I get the error: and it does work as long as you test for the following message as an acceptable case: "Did not find opening bracket after prefix: "[^\[]*", detected at offset 1216". The message just means you have passed all the brackets, which is fine.

my $next; while ( $next = (extract_bracketed($text,'[]','[^\[]*'))[0] ) { print "found matching brackets: *$next*\n"; } print "found bracket error: $@\n" if $@;

Replies are listed 'Best First'.
Re: Re: OS X vs. My Regex
by Pedro Picasso (Sexton) on Apr 17, 2003 at 15:57 UTC
    Thanks. Your example was helpful, and I'd never heard of Text::Balanced before. My expressions will not be such hogs in the future.
    -the Pedro Picasso
    (sourceCode == freeSpeech)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://251003]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2023-11-28 23:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?