2011-10-23 3 views
0

현재 코드 :정규 표현식을 사용하여 링크를 얻는 방법은 무엇입니까?

public static void WhoIsOnline(string worldName, WhoIsOnlineReceived callback) 
    { 
     string url = "http://www.tibia.com/community/?subtopic=worlds&world=" + worldName; 
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); 

     request.BeginGetResponse(delegate(IAsyncResult ar) 
     { 
      string html = GetHTML(ar); 

      MatchCollection matches = Regex.Matches(html, @"<TD WIDTH=70%><[^<]*>([^<]*)</A></TD><TD WIDTH=10%>([^<]*)</TD><TD WIDTH=20%>([^<]*)</TD></TR>"); 
      List<CharOnline> chars = new List<CharOnline>(matches.Count); 
      CharOnline co; 

      for(int i = 0; i < matches.Count; i++) 
      { 
       co = new CharOnline(); 
       co.Name = Prepare(matches[i].Groups[1].Value); 
       co.Level = int.Parse(matches[i].Groups[2].Value); 
       co.Vocation = Prepare(matches[i].Groups[3].Value); 
       chars.Add(co); 
      } 

      callback(chars); 
     }, request); 
    } 

내가 온라인 목록을 긁어이를 사용했지만, 그들은 자신의 레이아웃을 변경하고 내가 같은 정보를 얻을 수있는 정규 표현식을 변경하는 방법을 모르겠어요.

http://www.tibia.com/community/?subtopic=worlds&world=Libera

내가 위에서 사용하려고 링크.

+4

? [HTML 민첩성 팩] (http://htmlagilitypack.codeplex.com/)을보고 필요로하는 것을보다 강력하게 수행합니다. – Tomalak

+0

플레이어 이름, 직업 및 레벨을 검색하려고합니다. 민첩성 팩은이 작업을보다 쉽게 ​​수행 할 수 있습니까? –

+0

@Ales 예. 그리고 실수 방지. 그리고 더 많은 유지 보수가 가능합니다 (특히 정규 표현식이 당신의 강점이 아닌 것 같습니다). 아마 적은 코드 줄에서도 가능할 것입니다. 민첩성 팩의 작동 방식에 대한 개요는 [이 질문] (http://stackoverflow.com/questions/846994/how-to-use-html-agility-pack)을 참조하십시오. – Tomalak

답변

0

다른 말처럼 적절한 HTML 구문 분석은 훨씬 강력하고 확실히 더 좋은 방법입니다. 그러나

이 작동합니다 : 당신은 왜 HTML을 구문 분석 정규식을 사용하는

MatchCollection matches = Regex.Matches(html, @"<a href="".*?subtopic=characters&name=.*?"".*?>(.*?)</a>.*?<td.*?>(\d+)</td><td.*?>(.*?)</td>); 
관련 문제