![](/img/trans.png)
[英]Python re (regex) matching particular string containing letters, hyphen, numbers
[英]Regex in Python to get string of numbers after string of letters
我有一個格式為results_item12345
的字符串。 數字部分長度為四位或五位。 字母將始終為小寫,並且在非數字部分的某處始終會有下划線。
我嘗試使用以下方法提取它:
import re
string = 'results_item12345'
re.search(r'[^a-z][\d]',string)
但是,我只得到最左邊的兩位數。 我怎樣才能獲得整個號碼?
假設您只關心字符串末尾的數字,則以下表達式匹配字符串末尾的4或5位數字。
\d{4,5}$
否則,以下將是與提供的要求匹配的完整正則表達式。
^[a-z_]+\d{4,5}$
如果您想匹配字符串中的任何數字,您可以搜索:
r'[\d]{4,5}'
如果您需要某種驗證,則需要使用:
r'^result_item[\d]{4,5}$'
import re
a="results_item12345"
pattern=re.compile(r"(\D+)(\d+)")
x=pattern.match(a).groups()
print x[1]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.