简体   繁体   中英

How to keep the delimiters in place when split a string with multiple delimiters?

import re

p = re.compile(r"([?.;])")

ss = re.split(p, 'This is a test? This is a test?good.bad')

for s in ss:
    print(s)

The result is:

This is a test
?
 This is a test
?
good
.
bad

I hope the result would be:

This is a test?
This is a test?
good.
bad

Why does it put the delimiter on another line?

EDIT: I think I understand why it did that. The question is how to produce the result I want.

You can join back the delimiters and preceding items:

 ss = re.split(p, 'This is a test? This is a test?good.bad')
 result = [ a+b for a, b in zip(ss[::2], ss[1::2]) ] + (ss[-1:] if len(ss) % 2 else [])

A comment said you must use the pattern p . Here's a way to join the pairs up after a split. zip_longest ensures an odd pairing works out by returning None for the second element, which is converted to an empty string if present.

import re
from itertools import zip_longest

p = re.compile(r"([?.;])")

ss = re.split(p, 'This is a test? This is a test?good.bad')

for a,b in zip_longest(ss[::2],ss[1::2]):
    print(a+(b if b else ''))

Output:

This is a test?
 This is a test?
good.
bad

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM