0

I want to combine two regular expressions in to one.

$exp1 =/<area.*?href="([^"]*)".*?[^>]*>/s;
$exp2=/<a.*?href="([^"]*)".*?[^>]*>/s';
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
user739350
  • 103
  • 2
  • 10
  • 1
    Combine how? Should a string match either of them or both of them? – Ilmo Euro Jan 30 '13 at 10:48
  • 1
    Do not use regular expressions to parse HTML – Bergi Jan 30 '13 at 10:48
  • What have you red? What have you done before post question? Before post question, about annoying regular expressions, please, read about regular languages, grammatics, search in google and stackoverflow – gaussblurinc Jan 30 '13 at 10:55

2 Answers2

1

First of all, I would seriously consider using a proper HTML parser before even touching these kinds of expressions.

That said, what you're after is probably this:

/<(?:a|area).*?href="([^"]*)".*?[^>]*>/s

The (?:a|area) expression is an alternation between a and area; it's wrapped inside (?: ... ) to group the alternation and treat it as a non-capturing subpattern.

See also: subpatterns, alternation

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • Actually, if you're using regex for this, you should at least use word boundaries. Otherwise, you don't need the `area` alternation at all since `a.*?` will also match `area`: `/<(?:a|area)\b.*?href="([^"]*)".*?[^>]*>/s` – Tim Pietzcker Jan 30 '13 at 11:22
0

I think this should do it:

$exp =/<(?:area|a).*?href="([^"]*)".*?[^>]*>/s;

Btw, it's a bad idea to attempt to parse [X]HTML with regex.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375