简体   繁体   English

正则表达式以匹配折线

[英]Regular Expression to match break line

I'm looking for a regular expression to delete htmlA. 我正在寻找删除htmlA的正则表达式。 The border start from 边界从

<{if $data.nowPage>1}> to /div> 1 breakLine <{/if}> <{if $ data.nowPage> 1}>/ div> 1 breakLine <{/ if}>

htmlA HTMLA

<{if $data.nowPage>1}>
<div class="pageArrow pageFirst"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=1"><span class="icon arrow-first"></span></a></div>
<div class="pageArrow pagePrev"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=<{$data.nowPage-1}>"><span class="icon arrow-left"></span></a></div>
<{/if}>

I have tried 我努力了

$fullHtml = readFileData($file);
preg_match("/(<{if \\\$data\.nowPage>1}>(.*?)<\/div>)(\s)(.+?)<{\/if}>/si",$fullHtml,$htmlA);
//$fullHtml = preg_replace(...,'',$fullHtml);

but it stopped too early and why print_r($htmlA) shows 5 keys? 但是它停止得太早了,为什么print_r($ htmlA)显示5个键?

Array
(
    [0] => <{if $data.nowPage>1}>
        <div class="pageArrow pageFirst"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=1"><span class="icon arrow-first"></span></a></div>
        <div class="pageArrow pagePrev"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}>
    [1] => <{if $data.nowPage>1}>
        <div class="pageArrow pageFirst"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=1"><span class="icon arrow-first"></span></a></div>
    [2] => 
        <div class="pageArrow pageFirst"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=1"><span class="icon arrow-first"></span></a>
    [3] => 
    [4] => 
        <div class="pageArrow pagePrev"><a href="?<{if !empty($data.years)}>y=<{$data.years}>
)

fullHtml. fullHtml。 line3 to line6. 第3到第6行。

<div class="pageContainer">
    <div class="pageBox">
        <{if $data.nowPage>1}>
        <div class="pageArrow pageFirst"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=1"><span class="icon arrow-first"></span></a></div>
        <div class="pageArrow pagePrev"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=<{$data.nowPage-1}>"><span class="icon arrow-left"></span></a></div>
        <{/if}>

        <{for $i=$data.nowPage-$data.pageFit to $data.totalPage}>

            <{if $data.nowPage == ($i)}>
                <div class="pageBtn active"><a href="javascript:void(0);"><{$i}></a></div>
            <{else if $i>($data.nowPage+$data.pageFit)}>
                <div class="pageMore"><span class="icon moreIcon"></span></div>
                <{break}>
            <{else if $i>0}>
                <{if $i==$data.nowPage-$data.pageFit && $i>1}>
                <div class="pageMore"><span class="icon moreIcon"></span></div>
            <{/if}>
                <div class="pageBtn"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=<{$i}>"><{$i}></a></div>
            <{else}>
            <{/if}>

        <{/for}>

        <{if $data.nowPage<$data.totalPage}>
        <div class="pageArrow pageNext"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=<{$data.nowPage+1}>"><span class="icon arrow-right"></span></a></div>
        <div class="pageArrow pageLast"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=<{$data.totalPage}>"><span class="icon arrow-last"></span></a></div>
        <{/if}>
    </div>
</div>

Thank you. 谢谢。

I have written a pattern for your task, given your sample input. 给定您的示例输入,我为您的任务写了一个模式。

Pattern: ~<{if \\$data\\.nowPage>1}>(?:\\s+<div.*?</div>)+\\s+<{/if}>~ 格式: ~<{if \\$data\\.nowPage>1}>(?:\\s+<div.*?</div>)+\\s+<{/if}>~

Pattern Demo including leading and trailing whitespace adjustment 模式演示,包括前导和尾随空白调整

Code: ( Demo ) 代码:( 演示

echo preg_replace('~<{if \$data\.nowPage>1}>(?:\s+<div.*?</div>)+\s+<{/if}>~','',$html);
  • match the leading if line 匹配领先的if线
  • match one or more <div> lines 匹配一个或多个<div>
  • match the closing if line 匹配结束符if

This is your RegEx but you forgot to add a pattern to it and for this reason it stops as soon as a match is found: 这是您的RegEx,但是您忘记向其中添加模式,因此,一旦找到匹配项,它就会停止:

(<{if \\\$data\.nowPage>1}>(.*?)<\/div>)(\s)(.+?)<\/div>\s*<{\/if}>
                                                 ^^^^^^^^^^
  • \\s means any kind of whitespace \\s表示任何类型的空格

why print_r($htmlA) shows 5 keys? 为什么print_r($htmlA)显示5个键?

preg_match function returns first value in array as whole match and other indexes hold a value captured by a capturing group. preg_match函数在整体匹配时返回数组中的第一个值,而其他索引则包含捕获组捕获的值。 You have 4 capturing groups in your regex so it adds 4 more values in returning array which means total 5 keys. 您的正则表达式中有4个捕获组,因此它在返回数组中添加了4个值,这意味着总共有5个键。

Better approach 更好的方法

Regex: 正则表达式:

~<{if \$data\.nowPage>1}>.*?</div>\s*<{/if}>~si

Live demo 现场演示

PHP Code: PHP代码:

preg_match('~<{if \$data\.nowPage>1}>.*?</div>\s*<{/if}>~si', $str, $htmlA);

Could you please try this out. 您可以尝试一下吗。

 const string = `<{if $data.nowPage>1}> <div class="pageArrow pageFirst"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=1"><span class="icon arrow-first"></span></a></div> <div class="pageArrow pagePrev"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=<{$data.nowPage-1}>"><span class="icon arrow-left"></span></a></div> <{/if}>`; console.log(string.match(/^<{if \\$data.nowPage>1}>\\n<div.*\\n*<div.*\\n*<\\/div>\\n<{\\/if}>/)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM