1

我用它(?<=alt)[\w\s\,\/\(\)\.]*来提取第一个替代文本。这很棒,但我想提取多个替代文本。我在视觉网络开膛手中使用正则表达式

我从中提取的代码是

<DIV id=ctl00_ContentRightColumn_CustomFunctionalityFieldControl1_ctl00_ctl00_woodFeatures class="woodFeaturesPanel woodFeaturesPanelSingle" sizcache="23614" sizset="0"><H2>Features:</H2>  <DIV sizcache="23614" sizset="0">  <UL sizcache="23614" sizset="0">  <LI sizcache="23386" sizset="0"><IMG alt="Information board at site" src="/PublishingImages/icon_infoboard.gif">  <LI sizcache="20558" sizset="0"><IMG alt="Parking nearby" src="/PublishingImages/icon_carparknear.gif">  <LI sizcache="23614" sizset="0"><IMG alt=Grassland src="/PublishingImages/icon_grassland.giF">  <LI sizcache="17694" sizset="0"><IMG alt="Is woodland creation site" src="/PublishingImages/icon_woodlandcreation.gif">  <LI sizcache="21680" sizset="0"><IMG alt="Mainly broadleaved woodland" src="/PublishingImages/icon_mainlybroadleaved.gif">  <LI sizcache="20704" sizset="0"><IMG alt="Mainly young woodland" src="/PublishingImages/icon_mainlyyoung.gif">  <LI>  <LI></LI></UL></DIV></DIV>
4

1 回答 1

0

没有语言这很难说,但是使用记忆模式你可以捕捉到你需要的东西:

/alt=(\w\S*|"([^"]*)")/

使用preg_match_all()它会产生以下结果:

Array
(
    [0] => Array
        (
            [0] => alt="Information board at site"
            [1] => alt="Parking nearby"
            [2] => alt=Grassland
            [3] => alt="Is woodland creation site"
            [4] => alt="Mainly broadleaved woodland"
            [5] => alt="Mainly young woodland"
        )

    [1] => Array
        (
            [0] => "Information board at site"
            [1] => "Parking nearby"
            [2] => Grassland
            [3] => "Is woodland creation site"
            [4] => "Mainly broadleaved woodland"
            [5] => "Mainly young woodland"
        )

    [2] => Array
        (
            [0] => Information board at site
            [1] => Parking nearby
            [2] =>
            [3] => Is woodland creation site
            [4] => Mainly broadleaved woodland
            [5] => Mainly young woodland
        )

)

第二种内存模式用于双引号括起来的字符串;如果为空,您应该查看第一个内存模式。

于 2012-10-12T09:11:58.173 回答