Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Regular Expression Help

by haukex (Archbishop)
on Apr 24, 2020 at 17:24 UTC ( [id://11116032]=note: print w/replies, xml ) Need Help??


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
    Hi haukex, thank you for your insights. I used the DOM and I get the following results for https://finance.yahoo.com/quote/XOM?p=XOM&.tsrc=fin-srch
    ==> <span class="Trsdu(0.3s) Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(b)" + data-reactid="14">43.88</span> <== ==> <span class="Trsdu(0.3s) Fw(500) Fz(14px) C($positiveColor)" data- +reactid="16">+0.15 (+0.34%)</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="68">43.73</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="73">43.59</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="78">43.79 x 900</span> <= += ==> <span class="Trsdu(0.3s) " data-reactid="83">43.80 x 900</span> <= += ==> <span class="Trsdu(0.3s) " data-reactid="96">19,803,615</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="101">38,560,761</span> <= += ==> <span class="Trsdu(0.3s) " data-reactid="109">185.631B</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="114">1.27</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="119">13.07</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="124">3.36</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="143">47.13</span> <==
    My questions:
    sub { print "==> $_ <==\n" }
    How do I capture the $_ in a variable for example if I do this
    sub {push @myArray, $_; }
    It fails! I am trying to understand how I can capture the output in your sub into an array.
      sub {push @myArray, $_; } It fails! I am trying to understand how I can capture the output in your sub into an array.

      pushing the results onto an array works fine for me, so you'd have to show an SSCCE of how it's failing for you. However, note that you don't need to create a new array - the return value of the ->find method is a Mojo::Collection object, which is basically just a fancy array reference. In other words, you can do my $c = $dom->find(...) and then @$c is the array of results - an array of Mojo::DOM objects. Just a guess, but perhaps you need to look at those docs to see what you can do with such objects, such as for example calling their ->all_text method to get their contents. (You can use Perl's grep, map, and other array operations on @$c, or you can use Mojo::Collection's methods such as $c->grep(...) and $c->map(...), which have a different API and return Mojo::Collection objects. ->each is basically the object's version of Perl's foreach.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11116032]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found