2014-09-29 121 views
-2

我使用C#Regex從我的輸入中找到所有「http:// .....」。 這是我的代碼,但我沒有找到任何東西。請告訴我我錯過了什麼?「http *」正則表達式與URL匹配

Match m = Regex.Match(input, "http* "); 
while (m.Success) 
{ 
    Console.WriteLine("'{0}' found at index {1}.", 
    m.Value, m.Index); 
    m = m.NextMatch(); 
} 

這是我輸入文本(爲了便於閱讀):

I recently moved and have a buI recently moved and have a bunch of stuff for sale. 
Most prices are based on my research from CL and ebay. Let me know or make an offer 
if you like something from the list. Thanks. IKEA RAMBERG bed frame and Sultan 
mattress - $150 http://seattle.craigslist.org/est/fuo/4688883554.html Sanus Platinum 
Foundations TV Stand - $75 http://seattle.craigslist.org/est/fuo/4687613962.html 
Staples Mission Coffee table and 2 sets of nesting/side tables - $90 
http://seattle.craigslist.org/est/fuo/4687499215.html 
Like new Hoover SteamVac Carpet Cleaner with Clean Surge, F5914900 - $100 
http://seattle.craigslist.org/est/hsh/4687474666.html Hauppauge WinTV-HVR-1600 
ATSC/NTSC/QAM Tuner Video Card + Remote - $35 
http://seattle.craigslist.org/est/sop/4687372003.html Computer with core 2 quad, 2GB 
RAM, nforce MB, 1.5TB HDD and more - $200 http://seattle.craigslist.org/est/sys/4687362266.html 
LINKSYS CM100 Cable Modem (works with Comcast) - $15 
http://seattle.craigslist.org/est/ele/4687639722.html Various computer parts for sale - $1 I 
recently moved and have a buI recently moved and have a bunch of stuff for sale. Most prices 
are based on my research from CL and ebay. Let me know or make an offer if you like something 
from the list. Thanks. IKEA RAMBERG bed frame and Sultan mattress - $150 
http://seattle.craigslist.org/est/fuo/4688883554.html Sanus Platinum Foundations TV Stand - $75 
http://seattle.craigslist.org/est/fuo/4687613962.html Staples Mission Coffee table and 2 sets of 
nesting/side tables - $90 http://seattle.craigslist.org/est/fuo/4687499215.html Like new Hoover 
SteamVac Carpet Cleaner with Clean Surge, F5914900 - $100 
http://seattle.craigslist.org/est/hsh/4687474666.html Hauppauge WinTV-HVR-1600 ATSC/NTSC/QAM 
Tuner Video Card + Remote - $35 http://seattle.craigslist.org/est/sop/4687372003.html Computer 
with core 2 quad, 2GB RAM, nforce MB, 1.5TB HDD and more - $200 
http://seattle.craigslist.org/est/sys/4687362266.html LINKSYS CM100 Cable Modem (works with 
Comcast) - $15 http://seattle.craigslist.org/est/ele/4687639722.html Various computer parts for 
sale - $1 " 
+1

您缺少點。 – 2014-09-29 17:12:58

回答

1

對於初學者,請查看Stack Overdlow上的此前答案。 What is the best regular expression to check if a string is a valid URL?

看來你誤解了正則表達式的含義。

"http* " 

手段htt後跟0或多個p後跟一個空格。

*不像DOS或UNIX shell中的通配符fileglob。

在正則表達式*意味着零個或多個它如下(在這種情況下,這是p

爲了您輸入的目的令牌,你可以寫:

https?://(\S*) 

\ S捕獲所有非空間 ?使s可選,所以你可以抓住https以及

但是,對於任意輸入,空間並不總是唯一一個URL後面的東西。它可以用引號括起來,例如HTML或Javascript。後面應該允許一個URL後跟一個空格或一個非轉義引號。

https?://([^ "']*) 

使用^在的[]中的開始意味着圖案是獨佔模式(除了這些字符)和多次是寫圖案的最簡單的方法。另一種方法是編寫一個完全包容的模式,這意味着您必須爲您希望處理的每個合法輸入制定模式。

我不記得一個合規URL的實際正則表達式,它是不平凡的,但你可以在Google或Stack Overflow上找到一些。只是一般的想法,我可能會寫類似下面是一個包容性的模式:

https?://([-+a-zA-Z0-9._&?]*) 

如低於Lukos的評論指出,請記住C#逃逸。我通常在C#中使用逐字字符串作爲正則表達式。

var pattern = @"https?://\S*";

+0

實際上,他希望空間知道URL何時結束。空間是正確的。 – 2014-09-29 17:15:35

+0

@JonGrant - 當然,也許是爲了他的輸入樣本的目的,但通常情況下,空格不是唯一指示URL結束的分隔符。在任意HTML中,URL可以由其他東西分隔。不過,我會修改我的答案,以考慮他的輸入樣本。 – codenheim 2014-09-29 17:22:12

1

的問題是,你把一個星號*p後,你的表達"http* ",所以你可能的匹配是這樣的:

htt 
http 
httpp 
httppp 
httpppp 

等等。由於在輸入字符串中p之後沒有空格,因此您的表達式不會得到任何匹配。

該表達式應該匹配:

Match m = Regex.Match(input, "http\\S* "); 

\S裝置 「的任何非空白字符」)。

+0

@「http \ S *」可能更明顯(@符號表示你不是字符串轉義任何內容的正則表達式) – Lukos 2014-09-29 17:23:22

0

你的源代碼是尋找匹配此模式

"http* " 

這說來尋找序列htt,其次是零個或多個字符p,其次是文字空間('')字符。您可以嘗試匹配"http:[^\s]*",它將與文字文本http:相匹配,後跟零個或多個非空白字符。

0

在選擇使用正則表達式之前,有一個重要的問題。你是否想找到任何看起來像URL的東西(可能以http或https開頭),還是隻想匹配有效的URL?一個有效的URL正則表達式非常複雜,基本的一個更容易,但是你可能冒險收集文本中非URL的匹配,或者看起來像是真正的無效URL。

+0

對於一個簡單的問題,我會建議@「(http | https):// \ S *」,因爲它比包含http的文本更可能匹配真實的URL。 – Lukos 2014-09-29 17:27:07