如何在大字符串中找到 link
How to find a link in a big string
我想找一个link喜欢
在一个大字符串中,但有很多 link,我希望所有以 https://whosebug.com/questions/
开头的 link 字符串看起来像
某事
所以我的问题是如何找到不完整的字符串?
试试这个:
import re
def Find(string):
# findall() has been used
# with valid conditions for urls in string
regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
url = re.findall(regex,string)
return [x[0] for x in url if "whosebug.com/questions" in x[0]]
# Driver Code
string = 'content_license CC BY-SA 4.0 link https//whosebug.com/questions/26325943/many-threads-to-write-log-file-at-same-time-in-python title Many threads to writ'
print(Find(string))
这输出:['whosebug.com/questions/26325943/many-threads-to-write-log-file-at-same-time-in-python']
,这就是我假设你想要的。
我想找一个link喜欢 在一个大字符串中,但有很多 link,我希望所有以 某事 所以我的问题是如何找到不完整的字符串?
https://whosebug.com/questions/
开头的 link 字符串看起来像
试试这个:
import re
def Find(string):
# findall() has been used
# with valid conditions for urls in string
regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
url = re.findall(regex,string)
return [x[0] for x in url if "whosebug.com/questions" in x[0]]
# Driver Code
string = 'content_license CC BY-SA 4.0 link https//whosebug.com/questions/26325943/many-threads-to-write-log-file-at-same-time-in-python title Many threads to writ'
print(Find(string))
这输出:['whosebug.com/questions/26325943/many-threads-to-write-log-file-at-same-time-in-python']
,这就是我假设你想要的。