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 !
-
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.
|