[英]Simple python regex, match after colon
我有一個簡單的正則表達式問題,令我發瘋。 我有一個變量x = "field1: XXXX field2: YYYY"
。 我想檢索YYYY
(請注意,這是一個示例值)。 我的方法如下:
values = re.match('field2:\s(.*)', x)
print values.groups()
它不匹配任何東西。 我可以得到一些幫助嗎? 謝謝!
您的正則表達式很好
field2:\s(.*)
試試這個代碼
match = re.search(r"field2:\s(.*)", subject)
if match:
result = match.group(1)
else:
result = ""
re.match()
僅在字符串開頭匹配。 您想改用re.search()
。
另外,您應該使用逐字字符串:
>>> values = re.search(r'field2:\s(.*)', x)
>>> print values.groups()
('YYYY',)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.