in reply to Strip specific html sequence
Hi, you are using the match operator in void context where you want to use a string or a compiled regular expression in a variable assignment:
Or: (update2: ++Laurent_R pointed out that I had the quote operators reversed for string and substring in my OP):perl -wE ' my $remove = q{<div><div class="blue"></div></div>}; my $str = q{</div></div><div><div class="blue"></div></div>}; say $str =~ s/$remove//r;'
See http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators.perl -wE ' my $remove = qr{<div><div class="blue"></div></div>}; my $str = q{</div></div><div><div class="blue"></div></div>}; say $str =~ s/$remove//r;'
Note that if you were using warnings Perl would have told you about this:
perl -wE ' my $remove = m/<div><div class="blue"><\/div><\/div>/;' Use of uninitialized value $_ in pattern match (m//) at -e line 1.
(update) As far as your second question, you have to either get the result of the substitution in list context, or use the /r ("result") flag as I have showed above:
$ perl -wE ' my $remove = q{foo}; my $str = q{barfoobaz}; say $str =~ +s/$remove//;' 1 $ perl -wE ' my $remove = q{foo}; my $str = q{barfoobaz}; ( my $x = $s +tr ) =~ s/$remove//; say $x;' barbaz $ perl -wE ' my $remove = q{foo}; my $str = q{barfoobaz}; say $str =~ +s/$remove//r;' barbaz
Also note that you can use any character as quote delimiters in order to avoid "leaning toothpick syndrome" (<\/div>).
Finally, also note that there are modules for working with HTML parsing and processing, and trying to do it yourself with regular expressions is not generally recommended as you are unlikely to anticipate and handle all the edge cases.
Hope this helps!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Strip specific html sequence
by koober (Novice) on Dec 10, 2017 at 17:27 UTC | |
by AnomalousMonk (Archbishop) on Dec 10, 2017 at 18:15 UTC | |
by 1nickt (Canon) on Dec 10, 2017 at 18:01 UTC |