Why does it not work that way?

the regex metacharacter dot (.) means match any character ( except newline or including newline)

it starts to match after the first / is matched and it matches all subsequent /

This is a FAQ but hard to search for FAQ :)

use re 'debug'; and watch it work

$ perl -Mre=debug -le " $_ = q[ro/sham/bo]; print m{/(.+?)$} " Compiling REx "/(.+?)$" Final program: 1: EXACT </> (3) 3: OPEN1 (5) 5: MINMOD (6) 6: PLUS (8) 7: REG_ANY (0) 8: CLOSE1 (10) 10: EOL (11) 11: END (0) anchored "/" at 0 floating ""$ at 2..2147483647 (checking anchored) mi +nlen 2 Guessing start of match in sv for REx "/(.+?)$" against "ro/sham/bo" Found anchored substr "/" at offset 2... Found floating substr ""$ at offset 10... Starting position does not contradict /^/m... Guessed: match at offset 2 Matching REx "/(.+?)$" against "/sham/bo" 2 <ro> </sham/bo> | 1:EXACT </>(3) 3 <ro/> <sham/bo> | 3:OPEN1(5) 3 <ro/> <sham/bo> | 5:MINMOD(6) 3 <ro/> <sham/bo> | 6:PLUS(8) REG_ANY can match 1 times out of 1.. +. 4 <ro/s> <ham/bo> | 8: CLOSE1(10) 4 <ro/s> <ham/bo> | 10: EOL(11) failed... REG_ANY can match 1 times out of 1.. +. 5 <ro/sh> <am/bo> | 8: CLOSE1(10) 5 <ro/sh> <am/bo> | 10: EOL(11) failed... REG_ANY can match 1 times out of 1.. +. 6 <ro/sha> <m/bo> | 8: CLOSE1(10) 6 <ro/sha> <m/bo> | 10: EOL(11) failed... REG_ANY can match 1 times out of 1.. +. 7 <ro/sham> </bo> | 8: CLOSE1(10) 7 <ro/sham> </bo> | 10: EOL(11) failed... REG_ANY can match 1 times out of 1.. +. 8 <ro/sham/> <bo> | 8: CLOSE1(10) 8 <ro/sham/> <bo> | 10: EOL(11) failed... REG_ANY can match 1 times out of 1.. +. 9 <ro/sham/b> <o> | 8: CLOSE1(10) 9 <ro/sham/b> <o> | 10: EOL(11) failed... REG_ANY can match 1 times out of 1.. +. 10 <ro/sham/bo> <> | 8: CLOSE1(10) 10 <ro/sham/bo> <> | 10: EOL(11) 10 <ro/sham/bo> <> | 11: END(0) Match successful! sham/bo Freeing REx: "/(.+?)$"

use rxrx and watch it work

/ Match a literal '/' character ( The start of a capturing block ($1) .+? Match any character (except newline), one-or-mor +e times (as few as possible) ) The end of $1 $ Match only if at end of string (or final newline) .... $1 = 'sham/b' Back-tracked within regex and rematched | VVV //(.+?)$/ | V 'ro/sham/bo' ^^^^^^^ [Visual of regex at 'rxrx' line 0] [step: 35] ####### $1 = 'sham/bo' End of $1 | V //(.+?)$/ | V 'ro/sham/bo' \_____/ [Visual of regex at 'rxrx' line 0] [step: 36] ####### $1 = 'sham/bo' Testing if at end of string (or final newline) | V //(.+?)$/ | V 'ro/sham/bo' [Visual of regex at 'rxrx' line 0] [step: 37] ####### $1 = 'sham/bo' Assertion satisfied | V //(.+?)$/ | V 'ro/sham/bo' [Visual of regex at 'rxrx' line 0] [step: 38] ####### $1 = 'sham/bo' Regex matched in 39 steps | V //(.+?)$/ | V 'ro/sham/bo' [Visual of regex at 'rxrx' line 0] [step: 39]

In reply to Re^3: help with lazy matching ( .+? versus [^/]+? rxrx -Mre=debug ) by Anonymous Monk
in thread help with lazy matching by Special_K

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.