简体   繁体   English

如何匹配此字符串中的URL?

[英]How to match a URL in this string?

I've seen various articles which show how to match a URL. 我看过各种文章,展示了如何匹配URL。 But my situation is a bit different from the usual URL matching. 但是我的情况与通常的URL匹配有所不同。

This was one such regex that didn't work for me 这种正则表达式对我不起作用

/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/

My requirement: 我的要求:

My requirement is that I've a string like this 我的要求是我有一个像这样的字符串

userlist.2011.text_mediafire.com , userlist.2011.text_mediafire.com

userlist.2011.text_http://www.mediafire.com ", userlist.2011.text_http://www.mediafire.com ”,

userlist.2011.text_http://mediafire.com ", userlist.2011.text_http://mediafire.com ”,

userlist.2011.text.www.mediafire.com userlist.2011.text.www.mediafire.com

Now, I want to match mediafire.com along with (if exists) " http://www. " and " www. " so, the contraint that I wish to set is that all the strings to the left of a TLD (in this case '.com') should be recorded upto a list of specal characters like '"_- etc. 现在,我想将mediafire.com与“ http://www. ”和“ www。 ”(如果存在)匹配,因此,我要设置的矛盾之处是TLD左侧的所有字符串(在在这种情况下,“。com”)应记录到一个特殊字符列表中,例如'"_-等。

I wasn't able to proceed any further except that the basic /(.*)\\.(com|net|org|info)/ .Which is clearly wrong. 除了基本的/(.*)\\.(com|net|org|info)/以外,我无法进行任何其他操作,这显然是错误的。

使用以下正则表达式从组索引1中获取所需的字符串。

(?:http:\/\/)?(?:www\.)?([^'"_.-]*\.(?:com|net|org|info)\b)

You need the '$' to match the end of string. 您需要'$'来匹配字符串的结尾。 If you care about capturing the entire string before the special character you will also need to match the beginning of the string '^' . 如果您想在特殊字符之前捕获整个字符串,则还需要匹配字符串'^'的开头。

/^(.*)\.(([^\.]+)\.(com|net|org|info))$/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM