python - Regular Expression to use in Split function -
i new regular expressions. can me in regular expression split below data. tried regex \\\\,
splitting on comma inside braces , outside also. commas inside braces []
should skipped.
input
[111,212],[231,543],[231,423]
output
[111,212] [231,543] [231,423]
if sure there no spaces around commas, split regex want ,(?=\[)
example in javascript:
$ node > re = /,(?=\[)/ /,(?=\[)/ > "[111,212],[231,543],[231,423]".split(re) [ '[111,212]', '[231,543]', '[231,423]' ]
example in python:
$ python >>> import re >>> r = re.compile(r',(?=\[)') >>> re.split(r, "[111,212],[231,543],[231,423]") ['[111,212]', '[231,543]', '[231,423]']
explanation: ,(?=\[)
means comma followed left bracket. expression in (?=)
positive lookahead , not consumed. thing used splitter comma itself. commas split on ones followed left brackets. don't split on other commas.
Comments
Post a Comment