我有一個Python字符串如何在分隔符處拆分字符串,除非該分隔符後面跟有特定的模式?
string = aaa1bbb1ccc1ddd
,我想將它拆分這樣
re.split('[split at all occurrences of "1", unless the 1 is followed by a c]', string)
,這樣的結果是
['aaa', 'bbb1ccc', 'ddd']
我該怎麼辦呢?
我有一個Python字符串如何在分隔符處拆分字符串,除非該分隔符後面跟有特定的模式?
string = aaa1bbb1ccc1ddd
,我想將它拆分這樣
re.split('[split at all occurrences of "1", unless the 1 is followed by a c]', string)
,這樣的結果是
['aaa', 'bbb1ccc', 'ddd']
我該怎麼辦呢?
使用負先行用正則表達式和re
模塊:
>>> string = 'aaa1bbb1ccc1ddd'
>>> import re
>>> re.split(r"1(?!c)", string)
['aaa', 'bbb1ccc', 'ddd']
def split_by_delim_except(s, delim, bar):
escape = '\b'
find = delim + bar
return map(lambda s: s.replace(escape, find),
s.replace(find, escape).split(delim))
split_by_delim_except('aaa1bbb1ccc1ddd', '1', 'c')
雖然沒有漂亮的正則表達式,我下面的代碼返回相同的結果:
string = 'aaa1bbb1ccc1ddd'
p1 = string.split('1')
new_result = []
count = 0
for j in p1:
if j.startswith('c'):
# This removes the previous element from the list and stores it in a variable.
prev_element = new_result.pop(count-1)
prev_one_plus_j = prev_element + '1' + j
new_result.append(prev_one_plus_j)
else:
new_result.append(j)
count += 1
print (new_result)
[ 'AAA', 'bbb1ccc', 'DDD']
這個迭代器遍歷兩次(一次做'string.split ('1')'並且一次處理'p1')並且具有兩倍的存儲器佔用空間。正則表達式是更好的方式來做到這一點。 –
謝謝@MatthewCole,這很有意義。學習正則表達式真的很有用! –
使用負先行斷言: '1(?!C)'*(1後面沒有C)* –
're.split( 「1(?!三)」, 「aaa1bbb1ccc1ddd」)' – ozgur