首页 > 解决方案 > 正则表达式查找以下集合中不存在的所有匹配项

问题描述

如何对特定匹配中不存在的点符号执行正则表达式替换(例如在图像 HTML 标记中)

str = "Hi. the symbol before me is my match target <img src='http://skip.these.dots/and.this.as.well' alt='me.three' /> but still match this."

替换为(例如)* 符号

res = "Hi* the symbol before me is my match target <img src='http://skip.these.dots/and.this.as.well' alt='me.three' /> but still match this*"

标签: regexregex-groupregexp-replace

解决方案


试试这个:

\.(?![^<]*\/>)

演示

正则表达式引擎执行以下操作。

\.      # match period
(?!     # begin negative lookahead
  [^<]* # match 0+ chars other than '<'
  \/>   # match '/>'
)

/>如果在没有任何干预的情况下在句点之后遇到,则负前瞻将失败<,这意味着句点必须在 a<和 an之间/>

您希望用 替换句号的每个匹配项*。你如何做到这一点取决于你使用的语言,但这无疑是直截了当的。


推荐阅读