Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

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

Perl keeps the position of regexp-matching in mind: bound to a variable! See:

my $var = 'a111a222a333'; $var =~ m/a(\d+)/g and print $1 # prints '111'

The regex ended after the three 1es, at position 4. This position ist stored by perl inside tha variable. You can retrieve it via pos($var). Read on it in `perldoc -f pos`

$var =~ m/a(\d+)/g and print $1; #will now print '222' because the regex starts at # pos($var) (which is 4) #You can also use pos() as an lvalue pos($var) = 0; $var =~ m/a(\d+)/ and print $1; # NO, doesn't print '333' # but '111' because pos($var) wat set to 0 and so # the regexp startsat positin 0 again

Why do I tell you so? If you generate the strng again, the pos() will allways go back to 0, because you do not always match on the same variable, but on a new variable each time.

--
http://fruiture.de

In reply to Re: Re: Re: Infinite loop regex by fruiture
in thread Infinite loop regex by Baz

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 scrutinizing the Monastery: (4)
As of 2024-04-24 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found