簡體   English   中英

REGEX-(使用Python 3.5)-在文件中查找字符串

[英]REGEX - (using Python 3.5) - finding strings in file

我正在打開一個.msg前景文件,需要從中提取一些特定數據。 我對regex還是有點陌生​​,找不到我需要的東西。

以下是文件中的數據,其中包含一些選項卡,看起來像是fyi:

NEWS ID:    918273/1
TITLE:  News Platform Solution Overview (CNN) (US English Session)
ACCOUNT:    supernewsplatformacct (55712)

Your request has been completed.

Output Format   MP4

Please click on the "Download File" link below to access the download page.

Download File <http://news.downloadwebsitefake.com/newsid/file1294757493292848575.mp4>

我需要:

918273 -from- NEWS ID: 918273/1

News Platform Solution Overview (CNN) (US English Session) -from- TITLE: News Platform Solution Overview (CNN) (US English Session)

supernewsplatformacct -from- ACCOUNT: supernewsplatformacct (55712)

http://news.downloadwebsitefake.com/newsid/file1294757493292848575.mp4 -from- Download File <http://news.downloadwebsitefake.com/newsid/file1294757493292848575.mp4>

我正在努力

[\\n\\r][ \\t]*NEWS ID:[ \\t]*([^\\n\\r]*)

但是沒有運氣。 任何幫助將不勝感激!

(?:^|(?<=\n))[^:<\n]*[:<](.*)

您可以將其與re.findall一起使用。請re.findall演示。

https://regex101.com/r/d7RPNB/2

msg = """NEWS ID:    918273/1
TITLE:  News Platform Solution Overview (CNN) (US English Session)
ACCOUNT:    supernewsplatformacct (55712)

Your request has been completed.

Output Format   MP4

Please click on the "Download File" link below to access the download page.

Download File <http://news.downloadwebsitefake.com/newsid/file1294757493292848575.mp4>"""
import re
regex = r'[^:]+:\s+(.*)$|[^<]+<([^>]+)>'
matches = [re.match(regex, i).group(1) or re.match(regex, i).group(2) for i in msg.split('\n') if i and re.match(regex, i)]
print(matches)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM