3 回答

TA貢獻1886條經驗 獲得超2個贊
excluding = re.split('|'.join(including), s)
對于您知道包含信息將不包含特殊字符或正則表達式定義的簡單情況。
如果您不確定是否會出現這種情況:
re.split('|'.join(map(re.escape, including)), s)
這將轉義特殊的正則表達式字符,否則將導致re.split函數功能異常

TA貢獻1776條經驗 獲得超12個贊
使用正則表達式
a = re.findall('\)?[^()]*\(?', s)
excluded = a[::2]
included = a[1::2]
print(included, excluded, sep='\n')
['hahaha', 'hehehe', '']
['apple banana lemmon (', ') dog cat whale (', ') red blue black']
照顧空字符串
a = re.findall('\)?[^()]*\(?', s)
excluded = [*filter(bool, a[::2])]
included = [*filter(bool, a[1::2])]
print(included, excluded, sep='\n')
['hahaha', 'hehehe']
['apple banana lemmon (', ') dog cat whale (', ') red blue black']
沒有正則表達式
from itertools import cycle
def f(s):
c = cycle('()')
a = {'(': 1, ')': 0}
while s:
d = next(c)
i = s.find(d)
if i > -1:
j = a[d]
yield d, s[:i + j]
s = s[i + j:]
else:
yield d, s
break
included = []
excluded = []
for k, v in f(s):
if k == '(':
excluded.append(v)
else:
included.append(v)
print(included, excluded, sep='\n')
['hahaha', 'hehehe']
['apple banana lemmon (', ') dog cat whale (', ') red blue black']
相同想法但不覆蓋 s
from itertools import cycle
def f(s):
c = cycle('()')
a = {'(': 1, ')': 0}
j = 0
while True:
d = next(c)
i = s.find(d, j)
if i > -1:
k = a[d]
yield d, s[j:i + k]
j = i + k
else:
yield d, s[j:]
break
included = []
excluded = []
for k, v in f(s):
if k == '(':
excluded.append(v)
else:
included.append(v)
print(included, excluded, sep='\n')
['hahaha', 'hehehe']
['apple banana lemmon (', ') dog cat whale (', ') red blue black']

TA貢獻1825條經驗 獲得超6個贊
您可以使用正向后視和正向前瞻來拆分括號之間的單詞:
>>> re.split(r'(?<=\().*?(?=\))', s)
['apple banana lemmon (', ') dog cat whale (', ') red blue black']
添加回答
舉報