【问题标题】:Regex Negation on a set of strings一组字符串的正则表达式否定
【发布时间】:2012-01-19 00:08:05
【问题描述】:

在以下字符串中:

<table  border="1"><tr><td class=" m" bgcolor="#cccccc" style="bold" size="7" m="m "><span></span> </td><td class=" m" bgcolor="#cccccc" style="bold" size="7" m="m " align="center">NAMES </td><td class=" m" bgcolor="#cccccc" style="bold" size="7" m="m " colspan="13" align="center">AREAS </td></tr></table>

我想删除除以下之外的所有属性:边框、bgcolor 和类。

^(?:border|class|bgcolor)\b))=".*?"

不成功。请问有什么建议吗?

【问题讨论】:

  • 您可以为此使用正则表达式。但这仅适用于输出转换,而不是安全功能。正则表达式的开销太大,无法涵盖各种合法的序列化。

标签: php regex string regex-negation


【解决方案1】:
preg_replace('/ (?!(border|class|bgcolor))[^=" ]+="[^"]*"/', '', $str);

在此处查看实际操作:http://regexr.com?2vp0m


也就是说,please don't use a regex to parse HTML!!!

【讨论】:

  • +1 以获得好的答案(感谢 RegExr 的链接,我以前没见过 - 看起来很有用!)
  • @StuartGolodetz - 我在这里使用它来回答我关于正则表达式的所有答案。这是证明您所做的事情确实有效的最简单方法!
猜你喜欢
  • 2012-11-04
  • 1970-01-01
  • 2020-12-18
  • 2014-04-19
  • 2010-11-12
  • 1970-01-01
  • 1970-01-01
  • 2012-02-21
相关资源
最近更新 更多