views:

72

answers:

2

I'm trying to use WWW::Mechanize to extract some links from the HTML page using find_all_links() method. It supports matching on these criterias:

  • text
  • text_regex
  • url
  • url_regex
  • url_abs
  • url_abs_regex
    ...

How can I extract all links except one that has text "xyz"?

+6  A: 

You can use the 'text_regex' criteria:

$mech->find_all_links(text_regex => qr/^(?!xyz$).*$/);

See perldoc perlre for more on negative look-ahead assertion.

eugene y
Thanks, that worked
planetp
A: 

Why not get all links then use 'grep' to skip those you don't need?

Fayland Lam
Why not answer a question with a question?
Sinan Ünür
This approach is ok. In my script, params to find_all_links() are created dynamically. I find it easier to just use these params, w/o any special cases
planetp