【发布时间】:2018-02-08 13:11:19
【问题描述】:
我想提取以下文本中出现“ID”后的数字 这就是我能够得到它的方式。
import re
txt="Recharge done on 28-12-2017 04:57PM,MRP:Rs9.00,GST 18% payable by Company/Distributor/Retailer:Rs1.37, ID 147894886."
# 'ID' need to be present as mandatory group
regex = '(id)(.*?)(\d+})'
rg = re.compile(regex ,re.IGNORECASE|re.DOTALL)
m = rg.search(txt)
if m:
print m.group(3)
当我运行以下代码时,它会打印出来
147894886
问题来了
如果txt变成这样
txt="Recharge done on 28-12-2017 04:57PM,MRP:Rs9.00,GST 18% payable by Company/Distributor/Retailer:Rs1.37, TransID 147894886."
并且“ID”之前出现“Trans”字样,然后我不想提取数字。如何在正则表达式中执行此操作(即,如果“TransID”出现在数字之前,则不提取数字,但仅当“ID”出现时才提取数字)
【问题讨论】:
-
您是专门寻找
trans还是要确保id是一个完整的单词。如果是后者,请参阅stackoverflow.com/questions/1751301/… -
我要确保ID前的字符不应该是'(trans|trx|transc)'等
标签: python regex python-2.7 regex-negation regex-lookarounds