RegEx帮助,匹配长字符串,选择子字符串

时间:2011-08-17 22:41:12

标签: c# regex

我想匹配一个网址,但只想选择部分网址。这可能吗?

IE:网址看起来像这样:

http://www.domain.com/link/88cbdgb7fwndkedl/

My Regex看起来像这样:

@"http://www.domain.com/link/([a-zA-Z0-9]+)/"

我希望只返回id,而是抓住整个网址。

有什么建议吗?

编辑:使用c#Regex.Matches()

3 个答案:

答案 0 :(得分:1)

几乎所有正则表达式解决方案都允许您检索组。你会发现你的括号部分在第1组等你。

如果您无法打扰群体,那么您可以使用积极的后视:

(?<=http://www.domain.com/link/)[^/]+

但老实说......尝试使用群组。这是一个很好的功能。 : - )

在C#中,它是:

Match match = Regex.Match(input, @"http://www.domain.com/link/([a-zA-Z0-9]+)/");

if (match.Success) {
   string key = match.Groups[1].Value;
}

答案 1 :(得分:1)

Match match = Regex.Match(input, @"http://www.domain.com/link/([a-zA-Z0-9]+)/")
string key = match.Groups[1].Value;

答案 2 :(得分:0)

匹配将是整个网址(正则表达式中指定的内容)

您需要提取相应的组。

类似的东西:

foreach(Match m in matches){
m.Groups[1];
}
相关问题