Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正则表达式帮助,匹配长字符串,选择子字符串_C#_Regex - Fatal编程技术网

C# 正则表达式帮助,匹配长字符串,选择子字符串

C# 正则表达式帮助,匹配长字符串,选择子字符串,c#,regex,C#,Regex,我想匹配一个url,但只想选择url的一部分。可能吗 IE:url如下所示: http://www.domain.com/link/88cbdgb7fwndkedl/ 我的正则表达式看起来是这样的: @"http://www.domain.com/link/([a-zA-Z0-9]+)/" 我希望只返回id,但它会抓取整个url 有什么建议吗 编辑:使用c#Regex.Matches()匹配将是整个url(Regex中指定的内容) 您需要提取相应的组 比如: foreach(Match m

我想匹配一个url,但只想选择url的一部分。可能吗

IE:url如下所示:

http://www.domain.com/link/88cbdgb7fwndkedl/
我的正则表达式看起来是这样的:

@"http://www.domain.com/link/([a-zA-Z0-9]+)/"
我希望只返回id,但它会抓取整个url

有什么建议吗


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

匹配将是整个url(Regex中指定的内容)

您需要提取相应的组

比如:

foreach(Match m in matches){
m.Groups[1];
}

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

如果您无法处理组,则可以使用“积极的回头看”:

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

什么语言?问题不在于您的正则表达式,可能只是您使用的是整个匹配,而不是捕获的组。那么,
group[0]
是完整匹配吗,
group[1…等]
是连续组吗?
Match match = Regex.Match(input, @"http://www.domain.com/link/([a-zA-Z0-9]+)/");

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