$line=~ s/^ #Anchor at the beginning of the line logfile\s*=\s*([^,]*?) # As before (:?,.*)? # non-capturing optional block beginning with a comma $ # Anchor at end of line /$1/x;