110,536
社区成员
发帖
与我相关
我的任务
分享
(?i)(?<=^|http://(?:www\.)?)(?!www\.)\b\w+(?!\.\w+$)(?=(?:\.\w+)+)
Match m = Regex.Match(s, @"(?i)(?<=^|http://(?:www\.)?)(?!www\.)\b\w+(?!\.\w+$)(?=(?:\.\w+)+)");
string[] test = new string[] { "http://www.aa.bb.com", "http://aa.bb.com", "aa.bb.com", "http://www.bb.cn" };
foreach (string s in test)
{
Match m = Regex.Match(s, @"(?i)(?<=^|http://(?:www\.)?)(?!www\.)\b\w+(?!\.\w+$)(?=(?:\.\w+))");
if (m.Success)
{
richTextBox2.Text += "源字符串: " + s.PadRight(25, ' ') + "匹配结果: " + m.Value + "\n";
}
else
{
richTextBox2.Text += "源字符串: " + s.PadRight(25, ' ') + "匹配结果: 未匹配\n";
}
}
//输出
源字符串: http://www.aa.bb.com 匹配结果: aa
源字符串: http://aa.bb.com 匹配结果: aa
源字符串: aa.bb.com 匹配结果: aa
源字符串: http://www.bb.cn 匹配结果: 未匹配
string s1 = @"http://www.aa.bb.com";
string s2 = @"http://aa.bb.com";
string s3 = @"aa.bb.com";
string s4 = @"www.xxx.cn";
Regex re = new Regex(@"(?:http://)?(?:www)?\.?(?<a>\w+)..*", RegexOptions.Compiled);
if (re.Match(s1).Success && re.Match(s2).Success && re.Match(s3).Success && re.Match(s4).Success)
{
Console.WriteLine(re.Match(s1).Groups["a"].Value);
Console.WriteLine(re.Match(s2).Groups["a"].Value);
Console.WriteLine(re.Match(s3).Groups["a"].Value);
Console.WriteLine(re.Match(s4).Groups["a"].Value);
}
Regex re = new Regex(@"(?:http://)?(?:www)?\.?(?<a>\w+)..*", RegexOptions.Compiled);