[英]Python - Regex - combination of letters and numbers (undefined length)
我正在嘗試從文本文件中獲取文件 ID。 在上面的示例中,文件名是d735023ds1.htm
,我想獲取它以構建另一個 url。 這些文件名的長度不同,我需要一個通用的正則表達式來涵蓋所有可能性。
d804478ds1a.htm。
d618448ds1a.htm。
d618448.htm
for cik in leftover_cik_list:
r = requests.get(filing.url)
content = str(r.content)
fileID = None
for line in content.split("\n"):
if fileID == None:
fileIDIndex = line.find("<FILENAME>")
if fileIDIndex != -1:
trimmedText = line[fileIDIndex:]
result = RegEx.search(r"^[\w\d.htm]*$", trimmedText)
if result:
fileID = result.group()
print ("fileID",fileID)
document_link = "https://www.sec.gov/Archives/edgar/data/{0}/{1}/{2}.htm".format(cik, accession_number, fileID)
print ("Document Link to S-1:", document_link)
import re
...
result = re.search('^d\d{1,6}.+\.htm$', trimmedText)
if result:
fileID = result.group()
^d = 以廣告開頭
\\d{1,6} = 尋找 1-6 位數字,如果可以有無限數量的數字替換為 \\d{1,}
.+ = 通配符
\\.htm$ = 以 .htm 結尾
您應該嘗試re.match()
在輸入字符串的開頭搜索模式。 另外,你的正則表達式不好,你必須在.
,因為點在正則表達式中表示“任何字符”。
import re
result = re.match('[\w]+\.htm', trimmedText)
試試這個正則表達式:
import re
files = [
"d804478ds1a.htm",
"d618448ds1a.htm",
"d618448.htm"
]
for f in files:
match = re.search(r"d\w+\.htm", f)
print(match.group())
d804478ds1a.htm
d618448ds1a.htm
d618448.htm
上面的假設是文件名以d
開頭,以.htm
結尾,並且只包含字母、數字和下划線。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.