[英]Pattern match in file
我試圖在一個文件中找到多個匹配項。 我使用以下代碼:
f = open('/home/evi.nastou/Documenten/filename')
text = f.read()
#print text
urls = re.findall(r"_8o _8r lfloat\" href=\"(.+?)\" onclick=", text)
for url in urls:
print url.replace('\\','')
但不會返回任何結果。
另一方面,當我在變量中傳遞整個文本時,它確實找到了模式。 有人可以幫幫我嗎?
ps文件中文本的一部分:
問題似乎是您的正則表達式。
使用這個:
r'href\s*=\s*(.+)\s+onclick\s*='
碼:
import re
text = open('test.txt').read() # contains your string
urls = re.findall(r'href\s*=\s*(.+?)\s+onclick\s*=', text)
for url in urls:
print url.replace('\\','')
輸出:
"http://www.facebook.com/name"
我的正則表達式說明:
href # match href
\s* # match 0 or more spaces
= # match =
\s* # match 0 or more spaces
(.+?) # match any character (non - greedy)
\s+ # match 1 or more spaces
onclick # match onclick
\s* # match 0 or more spaces
= # match =
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.