2013-06-20 43 views
0

您好,我正在嘗試爲windows phone創建一個小webapp。 它拉下一個wepage並將源代碼保存在一個文本塊中,我想要做的是讓應用程序讀取該文本塊並確定是否包含設置值=「http://test.com」如果它確實需要它提取完整網址。查找字符串是否包含值,然後提取

到目前爲止,我有什麼是

 string stringToCheck = "http://test.com"; 
     string stringhtml = invisiBlockHtml.Text; 

     if (stringhtml.Contains(stringToCheck)) 
     { 
      // StreamReader sr = new StreamReader(stringhtml); 
      // here i was hoping there was some way for StreamReader or another function to read the string and then pull out the full url 



     webBrowser1.Navigate(new Uri("found Url", UriKind.Absolute)); } 

     else 
     { 
      webBrowser1.Navigate(new Uri("http://dontcontain.com", UriKind.Absolute)); 
     }` 

希望這是有道理的,我只有最近纔開始編程。 感謝

+0

看看正則表達式的http:/ /www.dotnetperls.com/regex-match – Jonesopolis

+0

你不能那樣做。我猜stringHtml可以是任何包含url的文本出現在任何地方。所以即使是正則表達式在這種情況下也無濟於事。 – cissharp

回答

0

這將試圖獲得一個URL匹配

string mystring = "My text and url http://www.google.com and some more text."; 

Regex urlRx = new Regex(@"(?<url>(http:[/][/]|www.)([a-z]|[A-Z]|[0-9]|[/.]|[~])*)", RegexOptions.IgnoreCase); 

MatchCollection matches = urlRx.Matches(mystring); 
+0

感謝隊友,即時只是試圖把它放在..如果我把正則表達式urlRx = new Regex(@「(?(http:[/] [/] | www.play)([az] | [AZ] | [0-9] | [/。] | [〜])*)「,RegexOptions.IgnoreCase);因爲sring中有多個網址,我只想要一個以play開頭的網址? – 9koni

+0

將網址設置爲正則表達式urlRx = new Regex(@「(?(http:[/] [/] | www.http://test.com)([az] | [AZ] | [0-9] | [/.]|~])*)「,RegexOptions.IgnoreCase); – Mohit

0

我評論,但這裏有一個整體正則表達式指南 - >

Regex Cheat Sheet爲C#

Regex Hero來測試你的正則表達式的好地方

Regex Matching如何從獲得匹配的值返回在C#中的正則表達式(你的問題)

編輯

雖然也許我誤解了這個問題。你正在尋找一個確切的網址,或任何網址?如果你正在尋找一個確切的網址,那麼如果。載有()返回true,你已經知道的URL

+0

謝謝我正在尋找一個網址,也有一個很長的qurystring,所以我知道的URL的第一部分,但希望找到qurystring。對不起,不解釋.. ..我會看看正則表達式謝謝.. :-) – 9koni

+0

這將是好的,爲此目的以及 – Jonesopolis

+0

很酷謝謝很多 – 9koni

相關問題