in reply to Matching optionally quoted string

This regex will work: m{<REF HREF="?(.*?)"?\s*/>}

Note that you do not have to escape the quotes. The only characters you have to escape are : \ | ( ) [ { ^ $ + ?

Update: Also . and * need to be escaped. (thanks Tachyon)

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Replies are listed 'Best First'.
Re: (2) Matching optionally quoted string
by ysth (Canon) on Dec 14, 2003 at 18:29 UTC
    The converse is equally important: you may escape any non-alphanumeric and be confident that it will literally match.

    { and } are a strange case: as long as you don't have a well formed {min[,[max]]} quantifier, you don't need to escape them. So if you always escape either { or } you are ok. People sometimes get in trouble by saying {,max} which is a literal match, not a quantifier.

    Update: _ seems to be a grey area. perlre seems to guarantee that \_ won't ever become a metacharacter, but quotemeta doesn't quote it.

Re: (2) Matching optionally quoted string
by ysth (Canon) on Dec 14, 2003 at 18:43 UTC
    You also need to escape @, and sometimes -. For the latter, consider this:
    $ perl -we'use overload q:"":=>sub {print "in stringify"}, q:%{}:=>sub + {print " in hash deref";{foo=>1}}; $x=bless[]; qr/$x\->{foo}/' in stringify sthoenna@DHX98431 ~ $ perl -we'use overload q:"":=>sub {print "in stringify"}, q:%{}:=>sub + {print " in hash deref";{foo=>1}}; $x=bless[]; qr/$x->{foo}/' in hash deref