首页 > 解决方案 > Regex to select all URLs in a body of text except a particular URL (Sublime Text)

问题描述

I have the following example of copy that I wish to do a find and replace in Sublime using Regex. However I cannot figure out how to select all the URLs except for a particular one. I know I can do it quite easily if I knew what the url was however the only URL I know of is the one I don't wish to replace with anchor tags.

Copy Example:

this is example.com.au and this is exampleflowers.com.au and of course another anotherexample.com.au/terms.html, url. Oh no exampleflowers.com.au is in this sentence again.

Ulimately I want any URL to be surrounded by a href tag except a URL which contains flowers.com.au in it!

My current simple Regex I use to test for URL is:

    /\w+(\.[^\s,\.^#]+)+/gi

I have also tried

    /\w+(?!flowers)(\.[^\s,\.^#]+)+/gi

Any assistance is deeply appreciated.

标签: regexpcre

解决方案


您的正则表达式将匹配 1+ 个单词字符\w+,然后重复 1+ 次捕获组(\.[^\s,\.^#]+)+,该组本身将匹配一个点和 1+ 次字符类中的内容。

否定前瞻(?!flowers)将在匹配 1+ 个单词字符的末尾检查是否flowers不在右侧,这将是正确的,因为它已经匹配了包括花在内的所有单词字符。

您可以将正则表达式与负前瞻结合使用,以检查右侧的内容是否不包含flowers.com.au

寻找

(?<!\S)(?!\S*flowers\.com\.au)(\w+(?:\.[^\s,.#]+)+)

代替

<a href="$1">$1</a>

解释

  • (?<!\S)否定后向断言左侧的内容不是非空白字符
  • (?!\S*flowers\.com\.au)断言右边的负前瞻不是 0+ 次非空白字符,后跟flowers.com.au
  • (\w+(?:\.[^\s,.#]+)+)在捕获组中使用您的正则表达式并在替换中使用它

正则表达式演示

请注意,您的否定字符类[^\s,\.^#]可以写为[^\s,.#]+


推荐阅读