Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
In other words (I thought) /(b*)/ stops after the first failure, at the start of the string,

s/failure/success/

whereas adding the /g would tell the regex engine to keep on trying until it reaches the end of the string.

What /g does on a regex depends on context. In boolean scalar context it matches once, and stores the position in pos. If you execute it a second time, it starts off from where it left.

The background is that you can write

while (/(b*)/g) { ... }

and get a new match for each iteration:

$perl -e '$_="abbbabc"; while (/(b*)/g) { print "($1)\n" }' () (bbb) () (b) () ()

Update: Answer to the second question

That this code produces two replacements for the string of four 'b's remains a puzzle. Why does this appear (this may be my error) that the regex conflates two 'b's rather than all four?

A naiive substitution implementation would loop on s/b*/^/, because it would continue to replace the empty string with ^ forever on.

Perl is a bit more sophisticated: It detects a zero-width match, and before doing a second substition of a zero-width match at the same position it bumps along, and tries in the next position.

So applying s/b*/^/ on abba make these steps:

abba | match zero b's before a ^abba | match zero b's again. Don't substitute here, bump along ^abba | match 'bb' ^a^a | match zero b's ^a^^a | match zero b's, don't substitute but bump along ^a^^a^ | match zero b's, don't substitute but bump along

You can watch it work; I didn't find a way to get the modified string, but at least you can monitor the match positions:

$ perl -le '$_ = "abba"; s/b*/print pos; "^"/eg; print' 0 1 3 4 ^a^^a^
Perl 6 - links to (nearly) everything that is Perl 6.

In reply to Re^2: Greediness of * vs. greediness of + by moritz
in thread Greediness of * vs. greediness of + by rovf

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 wandering the Monastery: (4)
As of 2024-03-29 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found