正则表达式匹配单词,除非它'是url的一部分
本文关键字:url 一部分 单词 正则表达式 | 更新日期: 2023-09-27 18:13:58
我正在尝试匹配一个单词的所有出现…除非它们出现在url中,否则我不想匹配它。假设我要匹配单词'co'
should match: cots and co
should not match: not http://me.co.uk
should match: last of my co.
我正试着用一个否定的眼神来做这件事,就像这样:
(?!http)'bco'b
'b
确保cots
中的co
不匹配,但它仍然匹配url中的co
。
(?!http).*'bco'b // This matches the whole sentence until the url
(?!http.*)'bco'b // this doesn't match anything
不确定是否消极的看后面是走的路,在我的情况下,它是安全的假设所有的url以http://
开头,所以这就是我试图开始的地方。
那么我如何匹配co
无处不在,除了在url?
我只想匹配单词,而不是整个句子
我在c#
编辑2:我想做的是将所有非url关键字转换为我博客上的内部url。
我现在得到了什么:
string orig = "co and http://me.co.uk";
string pros = Regex.Replace(orig, @"https?://'S*?co|'bco'b", m => string.Format("<a href='/co'>co</a>"));
(使用下面的答案)
当前给我
<a href='/co'>co</a> and <a href='/co'>co</a>.uk
and What I want is
<a href='/co'>co</a> and http://me.co.uk
谢谢
你可以在c#中使用这样的正则表达式:
(?<!https?://'S*)'bco'b
试试这个:
^(?!http'S+'.co('.|'?|$)).*'b(co)'b
匹配在组1中-参见这些输入的演示:
cots and co
last of my co.
http://me.co.uk
http://me.example.co
http://me.example.co?foo=bar