2011-12-09 4 views
0

私はこのために私の髪を引っ張ってきました:私は下のコードのLoactionブロックに含まれているテキストを抽出しようとしています。regexはhtmlタグの間に文字列を返しません

<h3 class="blue">Location</h3><p class="desc">This elegant luxurious hotel is located in the middle of stunning greenery on a hill, overlooking the sand/ pebble beach of Ixia, which is accessed just over the promenade (around 200 m away). The glamorous building, which is based on architecture from the Middle Ages is stylish and designed in classical, elegant decor. The island's capital of Rhodes Town is located around 4 km from the hotel and Rhodes' airport is roughly 9 km away whilst public transport departs from a stop located just 200 m away.</p>

<h3 class="blue">Location<\/h3><p\s(.*)\s.<\/p> 

と、それは動作しません:私はこれを抽出するために探しています。誰か助けてください。よろしくお願いします。

...In addition, there is also playground for younger guests in the hotel grounds.</p><h3 class="blue">Location</h3><p class="desc">This elegant luxurious hotel is located in the middle of stunning greenery on a hill, overlooking the sand/ pebble beach of Ixia, which is accessed just over the promenade (around 200 m away). The glamorous building, which is based on architecture from the Middle Ages is stylish and designed in classical, elegant decor. The island's capital of Rhodes Town is located around 4 km from the hotel and Rhodes' airport is roughly 9 km away whilst public transport departs from a stop located just 200 m away.</p><h3 class="blue">Rooms</h3><p class="desc">The comfortable rooms include an en suite bathroom with hairdryer, bathrobe, slippers, a direct dial telephone, satellite/ cable TV, a minibar, air conditioning (centrally regulated), a hire safe as well as a terrace or balcony.</p><h3 class="blue">Sports</h3><p class="desc">In the outdoor complex are 2 swimming pools with children's pools, a... 
+0

「は動作しません」を定義するためのグループをキャプチャするシンボル

  • (.*?)よりリテラル大きい
  • >一致。 –

  • +0

    のように、私はrubular.comでこれを試しているだけで、一致するとは言いません。 – user1038814

    答えて

    2

    ご使用の言語にHTMLを解析するライブラリがある場合は、そのライブラリを使用する必要があります。正規表現は常に最良のツールではありませんが、入力に精通していればそれを取り除くことが可能です。

    あなたのパターンは欲張りです。したがって、最初の終わりの段落タグを超えて一致します。貪欲でないようにするには、.*??の追加に注意してください)を使用する必要があります。

    さらにスラッシュをエスケープする必要はありませんが(あなたの履歴に基づいてPHPを使用していると推測されます)、\s.を使用するとテキストが終了しないため、一致が失敗します文字の後ろにスペースを入れてください。 .は、任意の文字に一致するメタ文字です。ピリオドにマッチすることを意図していた場合は、\.のようにリテラルにするためにエスケープする必要があります。

    pタグの後に\sを使用する代わりに、\bを使用して単語境界を示すことをお勧めします。最後に、段落テキストをキャプチャしない限り、キャプチャグループ(.*?)を使用する必要はありません。すべてのこれらの問題に対処するにはこれであなたを残します:

    <h3 class=\"blue\">Location<\/h3><p\b.*?<\/p> 
    

    あなたは段落テキストをキャプチャしたい場合は、このアプローチを取ることができる:

    <h3 class=\"blue\">Location<\/h3><p[^>]*>(.*?)<\/p> 
    
    • [^>]*より大きくない任意の文字にマッチシンボル、0回以上。パターンのこの部分の利点は、より大きいシンボルに遭遇するとすぐにマッチングが停止するため、非貪欲であるということです。内側の段落の内容
    +0

    ありがとうAhmad。これは本当に私を助けました。よろしく – user1038814

    0

    正規表現の末尾には\s.<\/p>があります。あなたのパラグラフの終わりはay.</p>です。 \sは空白文字と一致しますが、入力にyがあり、一致しません。

    0

    最初のグループの後に\sを削除するだけです。あなたはあなたの文字列にドットの前にスペースがありません。

    <h3 class="blue">Location<\/h3><p\s(.*).<\/p> 
    
    関連する問題