in reply to Re^4: I match a pattern in regex, yet I don't get the group I wanted to extract for some reason
in thread I match a pattern in regex, yet I don't get the group I wanted to extract for some reason
See Mojo::DOM::CSS for the selectors supported by Mojo::DOM; these are based on CSS selectors, which you can learn more about at various places on the Internet, for example https://www.w3schools.com/css/css_selectors.asp (though Mojo only supports a subset). Unfortunately, checking the text content of elements isn't something they can do, but you can still use the other methods in Mojo::DOM and Mojo::Collection to get what you want. So just for example, given your example input, the following code prints "11 December 2020".
$dom->find('.release-date-item') ->grep(sub { $_->at('.release-date-item__country-name') ->all_text =~ /^\s*USA\s*$/ }) ->each(sub { print $_->at('.release-date-item__date') ->all_text, "\n" });
Update: To clarify: It's ok that I'm using a regular expression here because I'm matching against the return value of ->all_text, which is just getting the plain-text content of the tags; I'm not trying to parse the tags themselves with the regex - Mojo::DOM has done that for us.
|
|---|