Regex에 이미 명명 된 그룹이 있습니다. ?<First>
구문은 그 괄호 내의 모든 것의 이름을 지정합니다 First
.
Regex
을 사용하여 일치하는 경우 Groups
속성을 사용하여 GroupCollection
에 액세스하고 이름별로 그룹 값을 추출하십시오.
var first = regex.Match(line).Groups["First"].Value;
이렇게하면 iTag에 대한 추가 그룹이 추가되지만 전체 URL은 유지됩니다. 다른 괄호 바깥으로 이동하여 변경하십시오.
(?<First>[,""]url=)(?<Url>[^\?]+?itag=(?<iTag>[0-9]*))(?<Last>\\u0026)
다음은 코드입니다.
Regex regex = new Regex("(?<First>[,\"]url=)(?<Url>[^\\?]*\\?itag=(?<iTag>[0-9]*))(?<Last>\\u0026)");
string input = ",url=http://domain.com?itag=25\u0026,url=http://hello.com?itag=11\u0026";
foreach(Match match in regex.Matches(input))
{
System.Console.WriteLine("1. "+match);
System.Console.WriteLine(" 1. "+match.Groups["First"]);
System.Console.WriteLine(" 2. "+match.Groups["Url"]);
System.Console.WriteLine(" 3. "+match.Groups["iTag"]);
System.Console.WriteLine(" 4. "+match.Groups["Last"]);
}
결과 :
1. ,url=http://domain.com?itag=25&
1. ,url=
2. http://domain.com?itag=25
3. 25
4. &
1. ,url=http://hello.com?itag=11&
1. ,url=
2. http://hello.com?itag=11
3. 11
4. &
확인이 사이트 아웃 : http://txt2re.com/index.php3 그것은 아마도 때문에 [^ \\] 전체 URL 넘어있는, 아무것도 캡처 아니에요 –