python - Context-sensitive string splitting, preserving delimiters -
I have a string of "foo-bar-1.23-4" form, and after the first hyphen one digit, The result is that ['foo-bar', '1.23-4]' I have tried the following:
& gt; & Gt; & Gt; Re.split ('- \ d', 'foo-bar-1.23-4', 1) ['foo-bar', '.23-4']
and
& gt; & Gt; & Gt; Re.split ('- (\ d)', 'foo-bar-1.23-4', 1) ['foo-bar', '1', '.23-4']
Can I get a liner without the extracting the delimiter with the last element with sub-corresponding results?
You were very close, try this:
re. Split ('- (? = = D)', 'foo-bar-1.23-4', 1)
I'm using it to complete - basically I Match a dash that is immediately followed by a numerical character.
Comments
Post a Comment