2014-09-29 128 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