簡體   English   中英

Python正則表達式從字符串中提取數據

[英]Python regex to extract data from string

我有一個文本文件,具有以下形式的行:

c="etc etc etc" 124:1 124:1||r="TrNAP etc"||c="etc etc" 124:10 124:10

引號中的文本逐行變化,數字也是如此。 否則格式不變。 數字表示其他文檔中引號中文本的行號和字號(line#:word#)

有人可以提供一些示例正則表達式代碼來提取line#:word#數字嗎? 謝謝!

>>> import re
>>> c = '"etc etc etc" 124:1 124:1||r="TrNAP etc"||c="etc etc" 124:10 124:10'
>>> print re.findall(r"(\d+):(\d+)", c)
[('124', '1'), ('124', '1'), ('124', '10'), ('124', '10')]

您可以使用以下內容:

(\d+:\d+)

演示

對於包含所有變量的完整行,請使用:

c="([^"]+)" (\d+):(\d+) (\d+):(\d+)\|\|r="([^"]+)"\|\|c="([^"]+)" (\d+):(\d+) (\d+):(\d+)

https://regex101.com/r/qY9kG2/1

暫無
暫無

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

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