Escapes within double quotes can be tricky. I suspect what you want is one of the following:

$regex = "href\\s*=\\s*\".*?\""; $regex = 'href\s*=\s*".*?"';

It might help you to print $regex and $match, to see what you are getting.

update: You can use $match in the substitution (at least it worked fine in my tests) but it won't do what you expect if $regex isn't what you expect. Also, since $match is one particular match, adding the global qualifier to the substitution using $match isn't likely to replace multiple instances.

You have indicated that you have more to do than just substitute the href values in the anchors. Without knowing more about what you are doing it is hard to make an appropriate recommendation but something like the following might be closer to what you want:

use strict; use warnings qw( FATAL all ); my $regex = 'href\s*=\s*".*?"'; print "regex = $regex\n"; foreach my $line (<DATA>) { chomp($line); while ($line =~ /$regex/) { my $match = $&; $line =~ s/$match/xxx/; } } __DATA__ No anchors here <a href="asdf">go there</a> <a href="fdsa">go here</a>

In reply to Re: Link Parser, something to be desired? by ig
in thread Link Parser, something to be desired? by koolgirl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.