Since you've chosen not to provide an example of something that doesn't work, I'll offer a few guesses at what you might be doing wrong:
Note that the delimiter (in your case
/
) cannot appear inside any comments inside the regex, because then they'll be indicating the end of the regex. For example, this:s/foo # this is interesting and/or cool /bar/x
will not work, because the regex is terminated by the slash between
and
andor
.Note that
/x
does not work on the replacement-string, only on the regex itself. For example this:s/foo/bar # I love the word bar/x
will replace
foo
withbar # I love the word bar
.If you really want to be able to put comments in the replacement-string, then I suppose you could use a replacement-expression instead, using the
/e
flag. That would let you use the full syntax of Perl. For example:s/foo/'bar' # I love the word bar/e
Here is an example that does work:
$test =~
s/
# the regex to replace:
(src\s*=\s*['"]?) # src=' or src=" (plus optional whitespace)
(.*?\.(jpg|gif|png)) # the URI of the JPEG or GIF or PNG image
/
# the string to replace it with:
$1 . # src=' or src=" (unchanged)
'something' . # insert 'something' at the start of the URI
$2 # the original URI
/sige;