Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

$my{'msg'}=~s/$name/\<img src=\"interface\/$image\"\>/g;
I get this error:

Unmatched ) before HERE mark in regex m/:) << HERE / at /var/securewww/virtual/webewebin.com/1/1/msgboard.cgi line 360.

Name is the smiley face and Image is the image url, but it might contain .'s and stuff.

Edit kudra, 2002-06-10 Changed title

Replies are listed 'Best First'.
Re: Help
by mirod (Canon) on Jun 10, 2002 at 15:26 UTC

    Here is how I would write this substitution:

    $my{'msg'}=~ s{\Q$name\E}{<img src="interface/$image">}g

    Explanations:

    • the most important is to use \Q$name\E: this preventsthe regexp engine to interpret meta-characters (such as /) in $name. Without this (admiteddly obscure) incantation the content of $name is interpolated, and used in the regexp,
    • < and " don't need to be backslashed,
    • also if you use a alternate delimiter to / (I often use s{}{}) you don't need to backslash /.

    Also note that "Help"is not a very... helpful title for a question. Posts are archived and can be searched, so using a descriptive like "problem with / in regexp" or something similar would make it easier for future users to retrieve and use your post. It would also make it more likely that people would take the time to answer your question.

Re: Error: unmatched ) before HERE mark in regex... (was: Help)
by Joost (Canon) on Jun 10, 2002 at 15:25 UTC
    You probably want
    $my{'msg'}=~s/\Q$name\E/<img src=\"interface\/$image\">/g;
    Check out
    perldoc perlre perldoc -f quotemeta
    -- Joost downtime n. The period during which a system is error-free and immune from user input.