簡體   English   中英

從 REGEX 結果中刪除括號和單引號

[英]Remove brackets and single quotes from REGEX result

我是 python 的新手。 我需要我的正則表達式在沒有括號和單引號的情況下返回

現在它返回: ['UP-3415']

我希望它返回: UP-3415

下面是我的代碼。

import re

str = "I need to extract UP-3415 from this string"
result = re.findall('UP-[0-9]{4}', str)
print(result)

提前致謝。

re.findall 返回所有與 List 匹配的結果。 因此,您可以將該值與result[0]一起使用,或者嘗試使用re.search

import re

str = "I need to extract UP-3415 from this string"
result = re.search('UP-[0-9]{4}', str)
print(result) # <re.Match object; span=(18, 25), match='UP-3415'>
print(result.group(0)) # UP-3415

暫無
暫無

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

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