in reply to Re^2: help with lazy matching
in thread help with lazy matching

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]