2012-04-19 2 views
0

他の2つの文字列の間に文字列を抽出したい。文字列はHTMLタグ内に存在しますが、正規表現でHTMLを解析する必要があるかどうかについての会話は避けたいと思います(stristr()を使用して問題を解決してはいけませんでしたが、 。正規表現で他の2つの既知の文字列の間で文字列を一致させる方法はありますか?

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

...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);

答えて

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]); 

出力

米国ワシントン座席を行います

+0

ありがとうございました。それは良い点であり、エラーを回避するが、それでも何も一致しない。 – codecowboy

+0

私はそれをテストし、私のために働いています。私は自分の答えを更新しました。 – stema

+0

ありがとうございます。それはローカルではありません。おそらく他の理由であろうが、私はその答えを受け入れるでしょう。 – codecowboy

関連する問題