1

I have a string '[123456] Filename123' and I want to get only the numbers inside [].

Input: [123456] Filename123, Output: ['123456'], I want 123456

My code:

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))
Slimey
  • 13
  • 2
  • Why don't you want `123` also? – Sweeper Feb 13 '20 at 10:43
  • 2
    You should provide more examples to know the cornercases. For example, does the strings always start with `[`? Are the numbers always between `[` and `]`? Do we need to skip finding numbers once we already found on in the string? – Adirio Feb 13 '20 at 10:47
  • Already kinda discussed here, check it out: https://stackoverflow.com/questions/14591258/regex-for-extracting-3-digits-numbers-between-brackets – stackMeUp Feb 13 '20 at 10:50

1 Answers1

2

You could use re.search with the following pattern:

import re
s =  '[123456] Filename123' 
re.search(r'\[(\d+)\]', s).group(1)
# '123456'
yatu
  • 86,083
  • 12
  • 84
  • 139