The reason is the "g" (global) option. The "g" option means that the next search will start where the last search succeeded. There is a counter associated with the string being searched that can be accessed using the "pos" function. The
search starts from "pos($regexp)". See a modified version of your code:
#!/usr/local/bin/perl
use strict;
use warnings;
my $regexp = 'Perl Monks';
if($regexp =~ m/^Perl Monks/gi) {
print "\nFound..";
} else {
print "\nNot Found..";
}
print "\npos = ".pos($regexp)."\n";
if($regexp =~ m/^Perl Monks/gi) {
print "\nFound.1.";
} else {
print "\nNot Found.1.";
}
when you run the code you obtain:
$ perl /tmp/prueba.pl
Found..
pos = 10
Since "pos" is now 10 the regexp will not match from that position.
Try to eliminate the "g" option the second time
and the regexp will succeed.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.