Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
2 つの正規表現を 1 つに結合したい。
$exp1 =/<area.*?href="([^"]*)".*?[^>]*>/s; $exp2=/<a.*?href="([^"]*)".*?[^>]*>/s';
まず第一に、これらの種類の式に触れる前に、適切な HTML パーサーを使用することを真剣に検討します。
そうは言っても、あなたが求めているのはおそらくこれです:
/<(?:a|area).*?href="([^"]*)".*?[^>]*>/s
式はとの(?:a|area)間の代替です。代替をグループ化し、それを非キャプチャ サブパターンとして扱うために内側にラップされます。aarea(?: ... )
(?:a|area)
a
area
(?: ... )
参照:サブパターン,交互
私はこれがそれを行うべきだと思います:
$exp =/<(?:area|a).*?href="([^"]*)".*?[^>]*>/s;
ところで、 [X]HTML を regex で解析しようとするのは悪い考えです。