Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I've been tinkering around with regular expressions, and in particular lookahead assertions. The examples on page 248 of the Camel (fourth edition) say that this code

"0123456789" =~ /(\d{3})/g;
returns only three strings, '012', '345' and '678', since the regex engine moves past the pattern it's found each time. I tried this in the debugger, and confirmed the behaviour:
main::(-e:1): 1 DB<1> $foo = "123456790" DB<2> @w1 = ( $foo =~ /(\d{3})/g ); DB<3> x @w1 0 123 1 456 2 790

Now, I'm interested in a regex that also finds '123' and '234' -- I want to tell the regex engine to reset to where it found the last match, plus one character. The Camel appears to say that the next example does this, with the lookahead assertion as follows:

"0123456789" =~ /(?:(\d{3}))/g;
Brimming with energy and thrilled at my discovery, I try that out in the debugger:
DB<4> @w2 = ( $foo =~ /(?:(\d{3}))/g ); DB<5> x @w2 0 123 1 456 2 790
Nope. Can someone suggest what I've done wrong?

Update: As noted below, this is a typo, as recorded on the errata page for this book. I'll be going through the rest of the errata and making updates to my Programming Perl (4th edition) later today.

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.


In reply to Lookahead assertion by talexb

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 sharing their wisdom with the Monastery: (5)
As of 2024-04-25 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found