0

I want to follow a link that contains $foo AND $bar.

I've tried this and it didn't work.

$mech->follow_link( url_regex => qr/$foo/i && url_regex => qr/$bar/i)

Robert
  • 11
  • 1
  • 1

1 Answers1

2

You could write a regex that matches both subpatterns:

qr/(?=.*?$foo)(?=.*$bar)/is

This uses two lookaheads that can match anywhere in the string, due to the .* prefix.

Note that this is more inefficient, and that the matched substring will differ.

amon
  • 57,091
  • 2
  • 89
  • 149
  • On the website i'm working on there will only be 1 link that contains both $foo and $bar in that order but there are other links that contain either $foo or $bar. Will this select that specific link? – Robert Aug 14 '13 at 17:30
  • If it will always be in the same order (`$foo` early in the string, `$bar` later), you can just use `qr/$foo.*$bar/i` and save a lot of effort. – AKHolland Aug 14 '13 at 17:47
  • @Robert What AKHolland said. My regex ANDs the two subregexes together (it matches if both parts match). If only one part has to match, simply use an alternation: `qr/$foo|bar/`. – amon Aug 14 '13 at 18:41