regex - 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.
解决方案
您的正则表达式将匹配 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,.#]+
推荐阅读
- python - python list of char append with +
- azure-devops - How to create Auto single Deployment release CD in azure DevOps for multiple instances of ScaleSet VM
- java.util.scanner - 如何正确使用 Math.log?
- javascript - 算法停止,我不知道为什么
- sql-server - 更新中的分组无法更新另一个表中的列
- azure - Azure Function v2 队列触发器仅接收 Json 消息
- arrays - 如何调用具有多个参数(包括二维数组)的方法
- c# - UrlEncoding 的 C# WebClient.DownloadFile 问题
- r - 为没有标题的数据生成线图
- docker - google kubernetes 集群上待处理的 spark pod:cpu 不足