简体   繁体   中英

Python Regular Expression Findall

I'm trying to find up to the top level domain information.

If I were to search " https://testwebsite.com.au/folders/viewforum.php?f=1556n " I only want my expression to find " https://testwebsite.com.au "

I'm using the following expression:

urlRegex = re.compile(r'''( (https?|sftp|ftp|file)://[-a-zA-Z0-9+&@#/%?
           =~_|!:,.;'"*$()]*[a-zA-Z0-9+&@#/%=~_|]   )''', re.VERBOSE)

If you want to be strict and correct, use a real URL parser. If you're looking for something quick and dirty that will work for 99% of the URLs you'll find, how about:

urlRegex = re.compile(r'([a-zA-Z]+://[^/\s]+)')

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