use re 'debug'; $m = ' more gibberish"h" \n URL="http://[10.0.0.3]?id=80943lkjh875kjrvf09u548gfpi"\n gibber\n'; $u= qr/(:?URL="([^"]*)")?/is; # optional clause (:?xxx)? if ($m =~ $u) { print "<$1><$2>\n"; }else{ print "no match u\n";} __END__ Compiling REx `(:?URL="([^"]*)")?' size 34 Got 276 bytes for offset annotations. first at 1 1: CURLYX[0] {0,1}(33) 3: OPEN1(5) 5: CURLY {0,1}(9) 7: EXACTF <:>(0) 9: EXACTF (12) 12: OPEN2(14) 14: STAR(26) 15: ANYOF[\0-!#-\377{unicode_all}](0) 26: CLOSE2(28) 28: EXACTF <">(30) 30: CLOSE1(32) 32: WHILEM(0) 33: NOTHING(34) 34: END(0) minlen 0 Offsets: [34] 18[1] 0[0] 1[1] 0[0] 3[1] 0[0] 2[1] 0[0] 4[5] 0[0] 0[0] 9[1] 0[0] 14[1] 10[4] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 15[1] 0[0] 16[1] 0[0] 17[1] 0[0] 18[0] 18[0] 19[0] Matching REx `(:?URL="([^"]*)")?' against ` more gibberish"h" \n URL="http://[10.0.0.3]?id=80943lkjh875...' Setting an EVAL scope, savestack=3 0 <> < more gibber> | 1: CURLYX[0] {0,1} 0 <> < more gibber> | 32: WHILEM 0 out of 0..1 cc=140fc18 Setting an EVAL scope, savestack=9 0 <> < more gibber> | 3: OPEN1 0 <> < more gibber> | 5: CURLY {0,1} EXACTF <:> can match 0 times out of 1... Setting an EVAL scope, savestack=9 failed... restoring \1..\2 to undef failed, try continuation... 0 <> < more gibber> | 33: NOTHING 0 <> < more gibber> | 34: END Match successful! Freeing REx: `"(:?URL=\"([^\"]*)\")?"' #### use YAPE::Regex::Explain; $u= qr/(:?URL="([^"]*)")?/is; # optional clause (:?xxx)? print YAPE::Regex::Explain->new($u)->explain; __END__ The regular expression: (?is-mx:(:?URL="([^"]*)")?) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?is-mx: group, but do not capture (case-insensitive) (with . matching \n) (with ^ and $ matching normally) (matching whitespace and # normally): ---------------------------------------------------------------------- ( group and capture to \1 (optional (matching the most amount possible)): ---------------------------------------------------------------------- :? ':' (optional (matching the most amount possible)) ---------------------------------------------------------------------- URL=" 'URL="' ---------------------------------------------------------------------- ( group and capture to \2: ---------------------------------------------------------------------- [^"]* any character except: '"' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \2 ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- )? end of \1 (NOTE: because you're using a quantifier on this capture, only the LAST repetition of the captured pattern will be stored in \1) ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------