내가 문자열을 가지고이 정규 표현식을 얻을 수없는 것.아래
var pattern = new System.Text.RegularExpressions.Regex(">(?<name>.+?)<");
가 나는 정규 표현식처럼 정말
내가 문자열을 가지고이 정규 표현식을 얻을 수없는 것.아래
var pattern = new System.Text.RegularExpressions.Regex(">(?<name>.+?)<");
가 나는 정규 표현식처럼 정말
그룹을 사용하지 않기 때문에 내가 놓친 게 무엇
>Joel Werner<
일치하려면 내가 그들을 일치하지만 내가 얻을 이름 :
var name = pattern.Match(input).Groups["name"].Value;
그룹을 참조하기 전에 Success
의 일치 항목을 확인할 수도 있습니다.
var match = pattern.Match(input);
if (match.Success)
name = match.Groups["name"].Value;
또한 Groups[1]
색인으로 그룹을 참조 할 수 있습니다.
사용이 정규식
<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1>
후 2 일치를 사용, 첫 경기는 태그 유형이다.
정규식이 마음에 들지 않으면이 경우에는 사용하지 마십시오. 정규 표현식으로 HTML을 파싱하는 것은 일반적으로 매우 나쁩니다. this answer on why을 참조하십시오. CsQuery를 사용
:
Console.WriteLine(CQ.Create("<a href=\"mailto:[email protected]\">Joel Werner</a>"). //create the selector
Attr("href"). //get the href attribute
Split(new char[]{':','@'})[1]); //split it by : and @ and take the second group (after the mailto)
는 XML에 LINQ에 내장 된 사용 :
XDocument doc = XDocument.Parse("<a href=\"mailto:[email protected]\">Joel Werner</a>");
Console.WriteLine(doc.Element("a").Attribute("href").ToString().Split(new char[] {':', '@'})[1]);
var input = "<a href=\"mailto:[email protected]\">Joel Werner</a>";
var pattern = new System.Text.RegularExpressions.Regex(@"<a\shref=""(?<url>.*?)"">(?<name>.*?)</a>");
var match = pattern.Match(input);
var name = match.Groups["name"].Value;
당신은 그 정규식에서 명명 된 캡처 그룹을 얻을 수 있습니다. –
아주 간단한 문자열을 파싱하지 않으면 정규식 대신 HTML 파서를 사용하는 것이 좋습니다. – Oded