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??

Hi Monks,

I played with regular expression and perl does something different as I would expect. Only the "+" operator worked as expected. When the "*" or "?" is at the end of a expression, is seems it is sometimes not greedy. Please see my example:

use strict; use warnings; $_="kkkaaabc"; print "String = $_\n"; print "1 $& Expected: ka ?? /k?a?/\n" if /k?a?/; print "2 $& Expected: kkka OK\n" if /k*a?/; print "3 $& Expected: kaaa OK\n" if /k?a+/; print "4 $& Expected: kkkaaa OK\n" if /k+a+/; print "5 $& Expected: kaaa ?? /k?a*/\n" if /k?a*/; print "6 $& Expected: kkkaaa OK\n" if /k*a*/;

The result is:

String = kkkaaabc 1 k Expected: ka ?? /k?a?/ 2 kkka Expected: kkka OK 3 kaaa Expected: kaaa OK 4 kkkaaa Expected: kkkaaa OK 5 k Expected: kaaa ?? /k?a*/ 6 kkkaaa Expected: kkkaaa OK

Why do I get in case 1 and 5 no "a" at the end of the matching circuit ??. Many Thanks for any help !


In reply to basic question: regular expression by Anonymous Monk

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 taking refuge in the Monastery: (6)
As of 2024-04-19 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found