kilojoules kilojoules - 3 months ago 11
Python Question

python regex to split list entry's name into list name and index

How can I use regular expressions to parse

<listname>[#]
into
[listname, #]
?

Here's what I've tried:

> s = 'li[10]'
> re.split(s,r'[%d]')
['[%d]']
> re.findall(s,r'[%d]')
[]
> s.split(r'[%d]')
['li[0]']


the desired output is
li
and
10

Answer

How about (.*)\[(.*)\]?

re.findall("(.*)\[(.*)\]", s)

# [('li', '10')]
Comments