![](/img/trans.png)
[英]I want to extract all decimal numbers from a string in python 3, how can I do that without using regex?
[英]How do I extract numbers only with regex?
我有一個字符串“[123456] Filename123”,我只想獲取 [] 中的數字。
輸入: [123456] Filename123
,輸出: ['123456']
,我想要123456
我的代碼:
from plexapi.server import PlexServer
import re
authType = 1
def getNumbers(str):
str = re.findall(r'[0-9].....', str)
return str
if authType == 1:
baseurl = 'http://localhost:32400'
token = 'token'
plex = PlexServer(baseurl, token)
pics = plex.library.section('Pictures')
print(pics.search())
for name in pics.search():
print(name.title)
print(getNumbers(name.title))
您可以使用具有以下模式的re.search
:
import re
s = '[123456] Filename123'
re.search(r'\[(\d+)\]', s).group(1)
# '123456'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.