add use re 'debug'; and see for yourself :) its because you're using m//g, m//atchingglobal, global flag , matching globally affects the pos the regex starts matching from , see pos

code with m//globally perl -Mre=debug -le " $_=q{fakesite.com/f/files-url/blah.png}; if(m{fakesite.com.*}g){ warn 1; if(/files-url/g){ die 2 }} "

code without m//globally perl -Mre=debug -le " $_=q{fakesite.com/f/files-url/blah.png}; if(m{fakesite.com.*}){ warn 1; if(/files-url/){ die 2 }} "

run

$ perl -Mre=debug -le " $_=q{fakesite.com/f/files-url/blah.png}; if(m{ +fakesite.com.*}g){ warn 1; if(/files-url/g){ die 2 }} " Compiling REx "fakesite.com.*" Final program: 1: EXACT <fakesite> (4) 4: REG_ANY (5) 5: EXACT <com> (7) 7: STAR (9) 8: REG_ANY (0) 9: END (0) anchored "fakesite" at 0 (checking anchored) minlen 12 Compiling REx "files-url" Final program: 1: EXACT <files-url> (5) 5: END (0) anchored "files-url" at 0 (checking anchored isall) minlen 9 Guessing start of match in sv for REx "fakesite.com.*" against "fakesi +te.com/f/files-url/blah.png" Found anchored substr "fakesite" at offset 0... Guessed: match at offset 0 Matching REx "fakesite.com.*" against "fakesite.com/f/files-url/blah.p +ng" 0 <> <fakesite.c> | 1:EXACT <fakesite>(4) 8 <esite> <.com/f/fil> | 4:REG_ANY(5) 9 <site.> <com/f/file> | 5:EXACT <com>(7) 12 <e.com> </f/files-u> | 7:STAR(9) REG_ANY can match 21 times out of 21 +47483647... 33 <url/blah.png> <> | 9: END(0) Match successful! 1 at -e line 1. Freeing REx: "fakesite.com.*" Freeing REx: "files-url" $ $ perl -Mre=debug -le " $_=q{fakesite.com/f/files-url/blah.png}; if(m{ +fakesite.com.*}){ warn 1; if(/files-url/){ die 2 } } " Compiling REx "fakesite.com.*" Final program: 1: EXACT <fakesite> (4) 4: REG_ANY (5) 5: EXACT <com> (7) 7: STAR (9) 8: REG_ANY (0) 9: END (0) anchored "fakesite" at 0 (checking anchored) minlen 12 Compiling REx "files-url" Final program: 1: EXACT <files-url> (5) 5: END (0) anchored "files-url" at 0 (checking anchored isall) minlen 9 Guessing start of match in sv for REx "fakesite.com.*" against "fakesi +te.com/f/files-url/blah.png" Found anchored substr "fakesite" at offset 0... Guessed: match at offset 0 Matching REx "fakesite.com.*" against "fakesite.com/f/files-url/blah.p +ng" 0 <> <fakesite.c> | 1:EXACT <fakesite>(4) 8 <esite> <.com/f/fil> | 4:REG_ANY(5) 9 <site.> <com/f/file> | 5:EXACT <com>(7) 12 <e.com> </f/files-u> | 7:STAR(9) REG_ANY can match 21 times out of 21 +47483647... 33 <url/blah.png> <> | 9: END(0) Match successful! 1 at -e line 1. Guessing start of match in sv for REx "files-url" against "fakesite.co +m/f/files-url/blah.png" Found anchored substr "files-url" at offset 15... Starting position does not contradict /^/m... Guessed: match at offset 15 2 at -e line 1. Freeing REx: "fakesite.com.*" Freeing REx: "files-url" $

shorter one

$ perl -Mre=debug -le "$f=123; $f=~/^.*/g; warn pos $f; $f=~ /\d/g; wa +rn pos $f; " Compiling REx "^.*" Final program: 1: BOL (2) 2: STAR (4) 3: REG_ANY (0) 4: END (0) anchored(BOL) minlen 0 Compiling REx "\d" Final program: 1: DIGIT (2) 2: END (0) stclass DIGIT minlen 1 Matching REx "^.*" against "123" 0 <> <123> | 1:BOL(2) 0 <> <123> | 2:STAR(4) REG_ANY can match 3 times out of 214 +7483647... 3 <123> <> | 4: END(0) Match successful! 3 at -e line 1. Warning: something's wrong at -e line 1. Freeing REx: "^.*" Freeing REx: "\d" $ perl -Mre=debug -le "$f=123; $f=~/^.*/; warn pos $f; $f=~ /\d/; warn + pos $f; " Compiling REx "^.*" Final program: 1: BOL (2) 2: STAR (4) 3: REG_ANY (0) 4: END (0) anchored(BOL) minlen 0 Compiling REx "\d" Final program: 1: DIGIT (2) 2: END (0) stclass DIGIT minlen 1 Matching REx "^.*" against "123" 0 <> <123> | 1:BOL(2) 0 <> <123> | 2:STAR(4) REG_ANY can match 3 times out of 214 +7483647... 3 <123> <> | 4: END(0) Match successful! Warning: something's wrong at -e line 1. Matching REx "\d" against "123" Matching stclass DIGIT against "123" (3 bytes) 0 <> <123> | 1:DIGIT(2) 1 <1> <23> | 2:END(0) Match successful! Warning: something's wrong at -e line 1. Freeing REx: "^.*" Freeing REx: "\d" $

In reply to Re: Why this Simple REGEX does Not Match? (re debug match globally pos) by Anonymous Monk
in thread Why this Simple REGEX does Not Match? by mmartin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.