代码之家  ›  专栏  ›  技术社区  ›  vishnuvardhanmalla

检测图像标记的正则表达式

  •  -1
  • vishnuvardhanmalla  · 技术社区  · 7 年前

    我正在尝试使用正则表达式

    <img __defa([^>]*)(.+?)my_macro_movingImage24Pro([^>]*)/> 
    

    用于以下字符串。 我需要检测3个包含“my\u macro\u movingImage24Pro”的图像字符串 理想情况下,我需要检索3个匹配项。但前2个匹配项重叠为1。 有人能帮忙吗?

    <body><p><strong>asifalsfhlshlsdasd</strong></p><p></p><p>Business
    aso;i;dfhd;sdafhsa;fhds;lfsaffas.</p><p></p><p><strong>Video
     I: safhsdlkfsddd</strong></p><p>asfsadfsafafsaadff.</p><p></p><p><img __default_attr="sfdsdfss" my_macro_name="sdfsdsd" class="my_macro my_macro_movingImage24Pro" data-renderedposition="239_8
    427_252" height="250" src="/dfsfds/sdfsd1619a91/images/sdfd/plugins/dfd/images/spacer.gif" width="425"/></p><p></p><p><strong>Video II: </strong><strong>sdfsadfsafs</strong><strong> (~ 3 min.)<br/></strong></p><p>dsfsdfsff.</p><p></p><p><img __default_attr="sfdsdfss" my_macro_name="sdfsdsd" class="my_macro my_macro_movingImage24Pro" data-renderedposition="575_8_427_252" height="250" src="/dfsfds/sdfsd1619a91/images/sdfd/plugins/dfd/images/spacer.gif" width="425"/></p><p></p><p><strong>Video III: </strong><strong>sdfasdfsadf</
    strong><strong> (~ 3 min.)</strong></p><p>Are you a people manager? asfsdafsaf.</p><p></p><p><img __default_attr="sfdsdfss" my_macro_name="sdfsdsd" class="my_macro my_macro_movingImage24Pro" data-renderedposition="911_8_427_252" height="250" src="/dfsfds/sdfsd1619a91/images/sdfd/plugins/dfd/images/spacer.gif" width="425"/></p><p></p><p>These videos are also posted on the <a _dser_internal="true" href="/sdf/asfdgcs/sfd/operations/sdfs-management"><strong>asfdsd Intranet page</strong> </a>dgfafdgadgg.</p><p></p><p>sfasfasdfasfaf <a href="XXXX" target="_blank"><strong>XXX</strong></a> (non-emergency on
    ly).</p></body>
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Jirka Picek    7 年前

    你的正则表达式几乎是正确的。有问题的部分是 (.+?) 。regex的这一部分可以匹配任何东西。下面的regex在上为我工作 regex101.com

    <img [^>]*my_macro_movingImage24Pro[^>]*\/>
    
        2
  •  0
  •   Susensio    7 年前

    第一个 img 标记分为两行,要么预处理文本以避免出现这种情况,要么使用 s 将输入视为单行的修饰符。

    (?s)<img.*?my_macro_movingImage24Pro.*?\/>
    

    live example in regex101