2011-05-17 73 views
46

有谁知道正则表达式我可以用它来找到一个字符串中的URL中找到的网址?我发现了很多关于谷歌正则表达式来确定是否整个字符串是一个URL,但我需要能够搜索整个字符串的URL。例如,我希望能够找到www.google.comhttp://yahoo.com以下字符串:正则表达式为字符串

Hello www.google.com World http://yahoo.com 

我不是在寻找字符串中的具体网址。我正在寻找字符串中的所有URL,这就是为什么我需要一个正则表达式。

+0

如果您有整个字符串的表达式,只需取^和$ out以使它们匹配字符串的部分。 – entonio 2011-05-17 22:55:13

回答

2

如果您有URL模式,你应该能够在你的字符串搜索。只要确保图案不必须^$标志着URL字符串的开头和结尾。因此,如果P是对URL的模式,寻找比赛为P.

+0

这是我发现的验证整个字符串是否为URL的正则表达式。我就像你说的那样,在开始和结束时拿出^,但它仍然不起作用。我究竟做错了什么? '^(HTTP | HTTPS | FTP)\:// [A-ZA-Z0-9 \ - \] + \ [A-ZA-Z] {2,3}(:[A-ZA- Z0-9] *)?/?([a-zA-Z0-9 \ - \。\ \?\,\'/ \\\ + &%\ $#\ =〜])* [^ \。\ ,\)\(\ s] $' – user758263 2011-05-17 23:19:58

+0

如果你显示了你正在使用的语言,它可能会有所帮助。无论哪种方式,一定要检查'http:// regexpal.com /';你可以测试不同的表达方式字符串,直到你得到它的权利 – entonio 2011-05-17 23:37:12

+0

@ user758263 - 你真的需要这样一个复杂的正则表达式的url吗?取决于你可能找到的可能的url。另请参阅http://gskinner.com/RegExr/尝试正则表达式他们也有在右边数百个样品的'Community'标签包括那些对于网址 – manojlds 2011-05-18 00:06:47

140

这是我使用的一个

(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\[email protected]?^=%&/~+#-])? 

对我的作品,应为你工作了。

+0

我试过,但它说“投票需要15声望”。对不起regexhacks :( – user758263 2011-05-20 20:56:53

+1

这是我所见过的最伟大的事情。你不知道,你到底有多少时间救了我。 – 2014-07-02 12:41:58

+0

的''&在表达腥。这哪里是应该在使用? – nhahtdh 2015-07-30 17:34:04

-1

我用找出两个点或时间段

之间的文本的逻辑下面的正则表达式正常工作与蟒蛇

(?<=\.)[^}]*(?=\.) 
0

这是/调整(这取决于你的需要)略有改善拉杰夫的回答是:

([\w\-_]+(?:(?:\.|\s*\[dot\]\s*[A-Z\-_]+)+))([A-Z\-\.,@?^=%&amp;:/~\+#]*[A-Z\-\@?^=%&amp;/~\+#]){2,6}? 

为它做什么和不匹配的例子见here

我摆脱了“http”等检查,因为我想赶上网址没有这个。我稍微在正则表达式中添加了一些混淆的url(即用户使用[dot]而不是“。”)。最后,我用“A-Z”替换了“\ w”和“{2,3}”以减少像v2.0和“moo.0dd”这样的误报。

对此欢迎的任何改进。

+0

'[a-zA-Z] {2,3}'对于匹配TLD确实很差,请参阅官方列表:https://data.iana.org/TLD/tlds-alpha-by-domain.txt。你的正则表达式匹配'_......... &&&&&&''不确定它是一个有效的url。 – Toto 2015-01-19 11:06:04

+0

感谢那个JE SUIS CHAELIE,有任何改进建议(特别是对于误报)? – avjaarsveld 2015-01-19 16:31:55

0

我用下面的正则表达式找到URL字符串中:

/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/ 
+0

'[a-zA-Z] {2,3}'对于匹配TLD确实很差,请参阅官方列表:https://data.iana.org/TLD/tlds-alpha-by-domain.txt – Toto 2015-01-19 11:04:15

15

想没有正则表达式是为这个完美的使用。我发现了一个非常坚实的here

/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/igm 

一些分歧/相比,这里张贴其他的优点:

  • 它确实匹配的电子邮件地址
  • 它确实匹配本地主机:12345
  • 它不会检测到类似moo.com没有httpwww

here的例子

+0

示例提供非常有说服力的 – 2015-07-12 14:59:43

+3

它匹配www.e 这不是一个有效的网址 – 2016-12-20 22:46:41

-1

这是最好的一个。

NSString *urlRegex="(http|ftp|https|www|gopher|telnet|file)(://|.)([\\w_-]+(?:(?:\\.[\\w_-]+)‌​+))([\\w.,@?^=%&:/~+#-]*[\\[email protected]?^=%&/~+#-])?"; 
2

上述所有的答案不匹配的URL Unicode字符,例如:http://google.com?query=đức+filan+đã+search

对于解决方案,这一项应该工作:

(ftp:\/\/|www\.|https?:\/\/){1}[a-zA-Z0-9u00a1-\uffff0-]{2,}\.[a-zA-Z0-9u00a1-\uffff0-]{2,}(\S*) 
+2

根据URL上的RFC 1738禁止Unicode字符(http://www.faqs.org/rfcs/rfc1738.html)。他们必须将百分比编码为符合标准 - 尽管我认为它最近可能已更改 - 值得阅读https://www.w3.org/International/articles/idn-and-iri/ – mrswadge 2016-09-07 09:41:49

+0

@mrswadge我刚刚案件。我们不确定是否所有人都关心标准。 感谢您的信息。 – 2016-09-12 02:54:53

-1

匹配一个URL中文字不应该这么复杂

(?:(?:(?:ftp|http)[s]*:\/\/|www\.)[^\.]+\.[^ \n]+)

https://regex101.com/r/wewpP1/2

+1

尝试用您的正则表达式找到“google.com”。 – Squazz 2016-12-20 12:22:41

-1

String regex = "[a-zA-Z0-9]+[.]([.a-zA-Z0-9])+";

这部作品在你的案例也没关系。

3

这里提供的解决方案中没有解决的问题/使用情况我了。

我在这里提供的,是我所发现的最佳/迄今所取得。当我发现它不处理的新边缘案例时,我会更新它。

\b 
    #Word cannot begin with special characters 
    (?<![@.,%&#-]) 
    #Protocols are optional, but take them with us if they are present 
    (?<protocol>\w{2,10}:\/\/)? 
    #Domains have to be of a length of 1 chars or greater 
    ((?:\w|\&\#\d{1,5};)[.-]?)+ 
    #The domain ending has to be between 2 to 15 characters 
    (\.([a-z]{2,15}) 
     #If no domain ending we want a port, only if a protocol is specified 
     |(?(protocol)(?:\:\d{1,6})|(?!))) 
\b 
#Word cannot end with @ (made to catch emails) 
(?![@]) 
#We accept any number of slugs, given we have a char after the slash 
(\/)? 
#If we have endings like ?=fds include the ending 
(?:([\w\d\?\-=#:%@&.;])+(?:\/(?:([\w\d\?\-=#:%@&;.])+))*)? 
#The last char cannot be one of these symbols .,?!,- exclude these 
(?<![.,?!-]) 
0

简单而简单。我没有在JavaScript代码中进行过测试,但看起来它的工作:

((http|ftp|https):\/\/)?(([\w.-]*)\.([\w]*)) 

Code on regex101.com

Code preview

0

如果你必须要严格的选择链接,我会去:

(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])) 

欲了解更多信息,请阅读:

An Improved Liberal, Accurate Regex Pattern for Matching URLs

+1

不要这样做。 http://www.regular-expressions.info/catastrophic.html 它会杀了你的应用程序... – Auric 2017-11-28 19:22:04

1

我想这正则表达式处理正是你想要什么

/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/ 

,这是一个片段为例,提取URL:

// The Regular Expression filter 
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; 

// The Text you want to filter for urls 
$text = "The text you want https://stackoverflow.com/questions/6038061/regular-expression-to-find-urls-within-a-string to filter goes here."; 

// Check if there is a url in the text 
preg_match_all($reg_exUrl, $text, $url,$matches); 
var_dump($matches); 
0

我用这个

^(https?:\\/\\/([a-zA-z0-9]+)(\\.[a-zA-z0-9]+)(\\.[a-zA-z0-9\\/\\=\\-\\_\\?]+)?)$ 
0

一个可能太简单但工作方法可能是:

[localhost|http|https|ftp|file]+://[\w\S(\.|:|/)]+ 

我测试了Python和只要字符串解析包含空格应该罚款之前和之后,并没有在URL(这是我以前从未见过)。

Here is an online ide demonstrating it

但是这里是使用它的一些好处:

  • 它承认file:localhost以及IP地址
  • 永远比赛没有他们
  • 它确实不介意不寻常的字符,如#-(请参阅本文的网址)
1
text = """The link of this question: https://stackoverflow.com/questions/6038061/regular-expression-to-find-urls-within-a-string 
Also there are some urls: www.google.com, facebook.com, http://test.com/method?param=wasd 
The code below catches all urls in text and returns urls in list.""" 

urls = re.findall('(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+', text) 
print(urls) 

输出:

[ 
    'https://stackoverflow.com/questions/6038061/regular-expression-to-find-urls-within-a-string', 
    'www.google.com', 
    'facebook.com', 
    'http://test.com/method?param=wasd' 
] 
0

这是一个最简单的一种。这对我很好。

%(http|ftp|https|www)(://|\.)[A-Za-z0-9-_\.]*(\.)[a-z]*%