Perl has so many ways of quoting things, you never have to escape characters.
1) Use the here doc syntax for your string, which will get rid of the outer double quote marks--allowing you to use double quote marks inside your string.
2) Use qr{ } around your whole regex, which will get rid of the beginning and ending forward slash marks(/)--which will allow you to use forward slash marks inside your regex.
3) Inside a regex, a single quote mark has no special meaning (same for a double quote mark), so you don't have to escape it.
4) It's usually more efficient to use:
[^some_char]some_char
than:
.*?some_char
Here is an example: use strict;
use warnings;
use 5.012;
my $string = <<END_OF_HTML;
<ul>
<li>
<div id="Show1400">
<a href="../../../KeyboardKeys/RetainerClip/Acer/Aspire/1400">14
+00</a>
</div>
</li>
<li>
<div id="Show1410">
<a href="../../../KeyboardKeys/RetainerClip/Acer/Aspire/1410">14
+10</a>
</div>
</li>
</ul>
END_OF_HTML
my $regex = qr{<a href=["']([^"']+)["']>[^<]+</a>};
while ($string =~ /$regex/g) {
say $1;
}
--output:--
../../../KeyboardKeys/RetainerClip/Acer/Aspire/1400
../../../KeyboardKeys/RetainerClip/Acer/Aspire/1410
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|