I'm trying to figure out a regex pattern to find all occurrences in a string for this:
string = "List[5] List[6], List[10], List[100:] List[-2:] List[-2]"
re.findall("List[(.*?)]" , string)
# Expected output ['5', '6', '10', '100:', '-2:', '-2']
# Output: []
What would be a good regex pattern to get the numbers in between the indexes?
Square brackets are special characters in Regex's syntax. So, you need to escape them:
>>> import re
>>> string = "List[5] List[6], List[10], List[100:] List[-2:] List[-2]"
>>> re.findall("List\[(.*?)\]", string)
['5', '6', '10', '100:', '-2:', '-2']
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With