Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
As GrandFather said, your regex is equivalent to /a*b/, so it's easy to see that the regex can never match. The slowdown is because of the backtracking done by Perl's regex engine.

Let's look at a smaller example:

"aaaa" =~ /a*a*b/
Except I'll make the following changes:
  • Instead of /b/ at the end, I'll use /(?!)/, which does the same thing here (it is a regex that always fails, just like the /b/ will always fail to match anywhere in this particular string).1
  • I'll capture both /a*/'s so I can look at what the regex engine puts in them
  • Before it gets to the end where it always fails (the /(?!)/ part), I'll have it print what parts of the string it tried matching to the /a*/'s.
The result is this:
"aaaa" =~ m/ (a*) (a*) (?{ print "Tried $`($1)($2)\n"; }) (?!) /x;
The output:
Tried (aaaa)() Tried (aaa)(a) Tried (aaa)() ... ... Tried aaa()(a) Tried aaa()() Tried aaaa()()
So you can see that it tries a way to fill $1 and $2 (and $`), fails to match /(?!)/, backtracks, and tries again repeatedly. It tries every possible way before the regex match operation finally returns with a failure. In this small example it tries 35 combinations before eventually failing.

Now if you make the string a lot longer, and give it more ways for the prefixes to match (more /a*/ regexes to fill), it will take a whole lot longer. By my calculations, the regex you gave above will try

24670925422945900903156716 (= 2e25)
combinations before eventually failing. Even at a billion combinations per second, that's still 782 million years ;)

1: If I left /b/ as the last part of the regex instead of /(?!)/, then the regex engine seemed to optimized away my print statement.

Update: revised my big number calculation, not that it makes a huge difference. It's 61+33 choose 33 if you're curious. That's the number of ways to put 61 balls (the a's) into 34 ordered bins (32 /a*/ regexes, plus $` and $').

blokhead


In reply to Re: slow regular expressions by blokhead
in thread slow regular expressions by jesuashok

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-20 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found