0

他の2つの文字列の間に文字列を抽出したいと思います。文字列はたまたまHTMLタグ内にありますが、正規表現を使用してHTMLを解析する必要があるかどうかについての会話は避けたいと思います(stristr()で問題を解決するべきではないことはわかっていますが、その方法を知りたいです)正規表現で。

文字列は次のようになります。

...uld select &#8220;Apply&#8221; below.<br/><br/><b>Primary Location</b>: United States-Washington-Seattle<br/><b>Travel</b>: Yes, 75 % of the Time <br/><b>Job Type</b>: Standard<br/><b>Region</b>: US Service Lines: ASL - Business Intelligence<br/><b>Job</b>: Business Intelligence<br/><b>Capability Group</b>: Con/Sol - BI&C<br/><br/>LOC:USA

<b>Primary Location</b>: United States-Washington-Seattle<br/>「米国-ワシントン-シアトル」に興味があり、抽出したい

'(?<=<b>Primary Location</b>:)(.*?)(?=<br/>)'はRegExrで動作しましたが、PHPでは動作しませんでした:

preg_match("/(?<=<b>Primary Location</b>:)(.*?)(?=<br/>)/", $description,$matches);

4

1 に答える 1

1

正規表現区切り文字として使用/したため、文字どおりに一致させるか、別の区切り文字を使用する場合はエスケープする必要があります

 preg_match("/(?<=<b>Primary Location</b>:)(.*?)(?=<br/>)/", $description,$matches);

preg_match("/(?<=<b>Primary Location<\/b>:)(.*?)(?=<br\/>)/", $description,$matches);

またはこれ

preg_match("~(?<=<b>Primary Location</b>:)(.*?)(?=<br/>)~", $description,$matches);

アップデート

www.writecodeonline.com/php でテストしたところ、

$description = "uld select “Apply” below.<br/><br/><b>Primary Location</b>: United States-Washington-Seattle<br/><b>Travel</b>: Yes, 75 % of the Time <br/><b>Job Type</b>: Standard<br/><b>Region</b>: US Service Lines: ASL - Business Intelligence<br/><b>Job</b>: Business Intelligence<br/><b>Capability Group</b>: Con/Sol - BI&C<br/><br/>LOC:USA";
preg_match("~(?<=<b>Primary Location</b>:)(.*?)(?=<br/>)~", $description, $matches);

print_r($matches);

は働いている。出力:

配列 ( [0] => 米国 - ワシントン - シアトル [1] => 米国 - ワシントン - シアトル )

キャプチャ グループを削除して実行することもできます。

$description = "uld select “Apply” below.<br/><br/><b>Primary Location</b>: United States-Washington-Seattle<br/><b>Travel</b>: Yes, 75 % of the Time <br/><b>Job Type</b>: Standard<br/><b>Region</b>: US Service Lines: ASL - Business Intelligence<br/><b>Job</b>: Business Intelligence<br/><b>Capability Group</b>: Con/Sol - BI&C<br/><br/>LOC:USA";
preg_match("~(?<=<b>Primary Location</b>:).*?(?=<br/>)~", $description, $matches);

print($matches[0]);

出力

米国-ワシントン-シアトル

于 2012-04-19T11:52:05.390 に答える