in reply to Regular Expression Help
When I fix the syntax errors (and write $string instead of string - always Use strict and warnings!), your second piece of code works for me, as in it prints "match". But you should not be Parsing HTML/XML with Regular Expressions! For one, the order of items in an HTML class attribute can change. Use something like Mojo::DOM, which supports selectors, instead.
use warnings; use strict; use Mojo::DOM; my $html = <<'END_HTML'; <span class="Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" data-reactid=" +33">a</span> <span class="D(ix) Mb(-4px) Fz(36px) Fw(b) Trsdu(0.3s)" data-reactid=" +34">b</span> <span class="Mb(-4px) Fz(36px) D(ib) Fw(b) Trsdu(0.3s)" data-reactid=" +35">c</span> END_HTML my $dom = Mojo::DOM->new($html); my $spans = $dom->find('span[class~="D(ib)"]')->each( sub { print "==> $_ <==\n" } ); __END__ ==> <span class="Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)" data-react +id="33">a</span> <== ==> <span class="Mb(-4px) Fz(36px) D(ib) Fw(b) Trsdu(0.3s)" data-react +id="35">c</span> <==
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regular Expression Help
by vskatusa (Acolyte) on Apr 27, 2020 at 18:45 UTC | |
by haukex (Archbishop) on Apr 27, 2020 at 20:31 UTC |