2014-10-28 65 views
0

喜字符串后更换的话,我有一个字符串这样的:查找单词和使用C#

string values = .....href="http://mynewsite.humbler.com.........href="http://mynewsite.anticipate.com..... and so on 

我需要找到。“mynewsite:关键字,然后更换‘COM’与‘网’ 有很多“com”出现在字符串中,所以我不能简单地使用values.Replace方法。 此外,除了“mysite”之外还有很多其他网站的介绍,所以我无法在http的基础上进行搜索......

+0

可以给我们一个厕所k在你的实际代码? – Toto 2014-10-28 09:58:58

+0

在你的字符串中,你是否有类似'mynewsite.humbler.com'的字符串,你是否想用'net'追加它们 – vks 2014-10-28 10:38:44

+0

不,我只想用net替换com,而且你的代码对我来说工作得很好。谢谢 – Aquarius24 2014-10-29 06:31:53

回答

0
(?<=http:\/\/mynewsite\.)(\w+\.)com 

试试这个。更换$1net。参见demo

http://regex101.com/r/sU3fA2/26

+0

它不适用于'mynewsite.humbler-with-dash.com' – Toto 2014-10-28 10:00:16

+0

@ M42已向OP询问相关问题 – vks 2014-10-28 10:38:59

0

由于C#正则表达式支持内部lookbehinds量词,你可以试试下面的正则表达式。然后用.net

@"(?<=(https?://)?(www\.)?(\S+?\.)?mynewsite(\.\S+?)?)\.com" 

例更换匹配.com

string str = @"....href=""http://mynewsite.humbler.com"" href=""www.foo.mynewsite.humbler.com"" foo bar href=""http://mynewsite.anticipate.com"" "; 
string result = Regex.Replace(str, @"(?<=(https?://)?(www\.)?(\S+?\.)?mynewsite(\.\S+?)?)\.com", ".net"); 
Console.WriteLine(result); 
Console.ReadLine(); 

输出:

....href="http://mynewsite.humbler.net" href="www.foo.mynewsite.humbler.net" foo bar href="http://mynewsite.anticipate.net" 

IDEONE