Not sure if there is an easy way to split the following string:
'school.department.classes[cost=15.00].name'
Into this:
['school', 'department', 'classes[cost=15.00]', 'name']
Note: I want to keep 'classes[cost=15.00]'
intact.
>>> import re
>>> text = 'school.department.classes[cost=15.00].name'
>>> re.split(r'\.(?!\d)', text)
['school', 'department', 'classes[cost=15.00]', 'name']
More specific version:
>>> re.findall(r'([^.\[]+(?:\[[^\]]+\])?)(?:\.|$)', text)
['school', 'department', 'classes[cost=15.00]', 'name']
Verbose:
>>> re.findall(r'''( # main group
[^ . \[ ]+ # 1 or more of anything except . or [
(?: # (non-capture) opitional [x=y,...]
\[ # start [
[^ \] ]+ # 1 or more of any non ]
\] # end ]
)? # this group [x=y,...] is optional
) # end main group
(?:\.|$) # find a dot or the end of string
''', text, flags=re.VERBOSE)
['school', 'department', 'classes[cost=15.00]', 'name']
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