S
sstark
Hi all, I know I will be told that I should be using one of the HTML
or XML parsers to perform this task, and you're right! but still
I'd like to know why my little regex while() loop isn't working.
Here's a sample XHTML code snippet that I'm working on:
For some reason, the s/^$prev//; isn't working; The result looks
something like this:
VALUE OF prev: ">(1)</a>, <a href="
BEFORE: ">(1)</a>, <a href="catalog.html#catalog">(2)</a>
AFTER: ">(1)</a>, <a href="catalog.html#catalog">(2)</a>
Why isn't it deleting the value of $prev in $line?
Meanwhile, yes I'm looking at one of the parsers.
thanks,
Scott
or XML parsers to perform this task, and you're right! but still
I'd like to know why my little regex while() loop isn't working.
Here's a sample XHTML code snippet that I'm working on:
Code:
<pre>
<li>description <a href="overview_mh.html#overview">(1)</a>, <a
href="catalog.html#catalog">(2)</a>
</pre>[code]
My goal:
1. Read a line of an XHTML file.
2. If it contains an href= attribute, grab everything up to the
opening href quote and print it out to a file.
3. Read the href attribute itself, change it and print the changed
version out to the file.
4. Delete everything from the beginning of the line up to the closing
quote of the href attribute.
5. Check to see if there's another href on the line; if so, repeat. If
not, print out the rest to the file.
Here's a code snippet:
[code]<pre>
while($line =~ /^(.*?href\s*=\s*\")([^\"]+)(\".*)/i){ #"
my $prev =$1;
my $href =$2;
print NEW $prev;
# do some stuff to the href
# ...
print NEW $href;
# remove both $prev and $href from $line and continue
print "VALUE OF prev: $prev\n";
print " BEFORE: $line";
$line =~ s/^$prev//;
print " AFTER: $line";
$line =~ s/^$href//;
}
print $line;
</pre>
For some reason, the s/^$prev//; isn't working; The result looks
something like this:
VALUE OF prev: ">(1)</a>, <a href="
BEFORE: ">(1)</a>, <a href="catalog.html#catalog">(2)</a>
AFTER: ">(1)</a>, <a href="catalog.html#catalog">(2)</a>
Why isn't it deleting the value of $prev in $line?
Meanwhile, yes I'm looking at one of the parsers.
thanks,
Scott