Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何组合这两种正则表达式模式?_Regex - Fatal编程技术网

Regex 如何组合这两种正则表达式模式?

Regex 如何组合这两种正则表达式模式?,regex,Regex,我觉得问这个问题很傻,但我不能用这个来救我的命 什么有效 preg_replace( '/(<[^>]+) onmouseout=".*?"/i', '$1', preg_replace( '/(<[^>]+) onmouseover=".*?"/i', '$1', $strHtml ) ) 更新:用于此线程未来视图的工具 我从来都不知道如何使用它,所以我尝试了这个regex101.com网站,从那以后我一直很喜欢它。强烈建议任何面临类似问题的人(像我最初那样使用剪切

我觉得问这个问题很傻,但我不能用这个来救我的命

什么有效

preg_replace( '/(<[^>]+) onmouseout=".*?"/i', '$1', preg_replace( '/(<[^>]+) onmouseover=".*?"/i', '$1', $strHtml ) )
更新:用于此线程未来视图的工具


我从来都不知道如何使用它,所以我尝试了这个regex101.com网站,从那以后我一直很喜欢它。强烈建议任何面临类似问题的人(像我最初那样使用剪切粘贴正则表达式模式…。

原始表达式的问题是初始组抓取太多,因此两个被替换的组中唯一一个是最后出现的组。这是因为贪婪的
[^>]+
重复占用了搜索字符串中比您预期的更大的部分,捕获了从第一个所需匹配的开始到您想要删除的第二个属性的所有内容。 然后将模式锚定到html标记的起始括号中,即使在解决该问题之后,也会阻止元素中的多个匹配

如果您想在一次调用
preg_replace()
中执行此操作,则与其尝试获取要保留的文本,不如查找要删除的文本(通过用空字符串替换):


您已经在属性值上进行了非贪婪匹配(使用
*?
),并且基于之前的代码,它似乎已经对您运行良好。请注意,这个特定的表达式并没有涵盖HTML/XML文档中所有可能的变体(例如,空格和引号)。我相信您可以判断这个表达式是否足够通用以满足您的需要。

Sigh。第一个
+
是贪婪的,将匹配最后一个
onmouseover
onmouseout
之前的所有内容。因此,如果它们出现在同一行上,第一个将不会被替换,也不会非常无助,但是我如何才能重新设计这个模式,从而达到预期的结果呢?我试着去掉那个加号,但没用?(正则表达式不是我喜欢的:()OP接受了答案,那么否决票又有什么用呢?因为它被应用于HTML?你的解决方案可行,但为什么?OP的正则表达式出了什么问题?没有这个解释,你的答案是不完整的。这很好,我会补充一些。但大部分内容都在早期评论中出现,OP很快就接受了答案。Bett呃,但问题并不是
[^>]+
部分的贪婪(
[^>]+?
也不起作用),而是
是的,你也是对的。OP在我回答后添加了示例,所以当时我们甚至不知道这都发生在一个html元素中。
preg_replace( '/(<[^>]+) (onmouseover|onmouseout)=".*?"/i', '$1', $strHtml )
<p><img src="http://www.bestlinknetware.com/products/204233spc.jpg" width="680" height="365"><br>   <a href="http://www.bestlinknetware.com/products/204233INST.pdf" target="_blank" onmouseover="MM_swapImage('Image2','','/Content/bimages/ins2.gif',1)" onmouseout="MM_swapImgRestore()"><img name="Image2" border="0" src="http://www.bestlinknetware.com/Content/bimages/ins1.gif"></a> </p> <p><strong>No contract / No subscription / No monthy fee<br> 1080p HDTV reception<br> 32db high gain reception<br> Rotor let you change direction of the antenna to find best reception</strong></p>  <a href=http://transition.fcc.gov/mb/engineering/dtvmaps/  target="blank"><strong>CLICK HERE</strong></a><br>to see HDTV channels available in your area.<br> <br/> ** TV signal reception is immensely affected by the conditions such as antenna height, terrain, distance from broadcasting transmission antenna and output power of transmitter. Channels you can watch may vary depending on these conditions. <br> <br/> <br/> <p>* Reception: VHF/UHF/FM<br/>   * Reception range: 120miles<br/>   * Built-in 360 degree motor rotor<br>   * Wireless remote controller for rotor (included)<br/>   * Dual TV Outputs<br>   * Easy Installation<br>   * High Sensitivity Reception<br>   * Built-in Super Low Noise Amplifier<br>   * Power : AC15V 300mA<br> <br/> Kit contents<br/> * One - HDTV Yagi antenna with built-in roter & amplifier<br/> * One - Roter control box<br/> * One - Remote for roter control box<br/> * One - 40Ft coax cable<br/> * One - 4Ft coax cable<br/> * One - power supply for roter control box</p>
preg_replace( '/(onmouseover|onmouseout)=".*?"/i', '', $strHtml )