简体   繁体   中英

How to match a URL in this string?

I've seen various articles which show how to match a URL. But my situation is a bit different from the usual URL matching.

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_http://www.mediafire.com ",

userlist.2011.text_http://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.

I wasn't able to proceed any further except that the basic /(.*)\\.(com|net|org|info)/ .Which is clearly wrong.

使用以下正则表达式从组索引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))$/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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