我不能选择 RegEx
I can not choose RegEx
<p[^>]*>([a-zA-Z0-9_\W]*)\:<\/p>.*?(<blockquote[^>]*>).*?<\/blockquote>
<p> demo demo:</p> <p ><img src="http://demo.com/123.jpg" width="100%"/> <br/> <em>Credit: demo2 demo2 </em></p> <p >here1 here1:</p> <blockquote cite="here1"> <p><em>demo3. demo3 demo3 demo3:</em></p> </blockquote> <p >demo4 demo4:</p> <p ><img src="http://demo.com/1234.jpg" width="100%"/> <br/> <em>demo5 demo 5 demo5</em></p> <p >demo6 demo6:</p> <blockquote cite="demo6"> <p><em>demo7 demo7<br/>
以上模式有问题。我哪里可以出错。我上面写的pattern,一句话选不出来我想要的。请求帮助。
我想得到结果:
<p >here1 here1:</p> <blockquote cite="here1"> <p><em>demo3. demo3 demo3 demo3:</em></p> </blockquote>
如果你真的想在这里使用正则表达式,这可能对你有用:
<p[^>]*>((?:(?!<\/p>).)+)<\/p>\s*<blockquote[^>]*>(.*?)<\/blockquote>
相关部分是((?:(?!<\/p>).)+)
:用英文表示,"look ahead to make sure there's no </p>
, then grab one character, and repeat this (until the next </p>
)"。这样,就不会匹配多个同级 <p>
(或者实际上是嵌套的 <p>
),这就是您的原始模式所发生的情况:<p[^>]*>([a-zA-Z0-9_\W]*)\:<\/p>
会错误地匹配在这种情况下:<p>one paragraph</p><p>second paragraph</p>
。我还在 <p>
和 <blockquote>
之间指定了空格(\s*
,而不是 .*?
),所以你只匹配 前面的 <p>
.
<p[^>]*>([a-zA-Z0-9_\W]*)\:<\/p>.*?(<blockquote[^>]*>).*?<\/blockquote>
<p> demo demo:</p> <p ><img src="http://demo.com/123.jpg" width="100%"/> <br/> <em>Credit: demo2 demo2 </em></p> <p >here1 here1:</p> <blockquote cite="here1"> <p><em>demo3. demo3 demo3 demo3:</em></p> </blockquote> <p >demo4 demo4:</p> <p ><img src="http://demo.com/1234.jpg" width="100%"/> <br/> <em>demo5 demo 5 demo5</em></p> <p >demo6 demo6:</p> <blockquote cite="demo6"> <p><em>demo7 demo7<br/>
以上模式有问题。我哪里可以出错。我上面写的pattern,一句话选不出来我想要的。请求帮助。
我想得到结果:
<p >here1 here1:</p> <blockquote cite="here1"> <p><em>demo3. demo3 demo3 demo3:</em></p> </blockquote>
如果你真的想在这里使用正则表达式,这可能对你有用:
<p[^>]*>((?:(?!<\/p>).)+)<\/p>\s*<blockquote[^>]*>(.*?)<\/blockquote>
相关部分是((?:(?!<\/p>).)+)
:用英文表示,"look ahead to make sure there's no </p>
, then grab one character, and repeat this (until the next </p>
)"。这样,就不会匹配多个同级 <p>
(或者实际上是嵌套的 <p>
),这就是您的原始模式所发生的情况:<p[^>]*>([a-zA-Z0-9_\W]*)\:<\/p>
会错误地匹配在这种情况下:<p>one paragraph</p><p>second paragraph</p>
。我还在 <p>
和 <blockquote>
之间指定了空格(\s*
,而不是 .*?
),所以你只匹配 前面的 <p>
.