2017-05-04 24 views
2

我有一個Python字符串如何在分隔符處拆分字符串,除非該分隔符後面跟有特定的模式?

string = aaa1bbb1ccc1ddd 

,我想將它拆分這樣

re.split('[split at all occurrences of "1", unless the 1 is followed by a c]', string) 

,這樣的結果是

['aaa', 'bbb1ccc', 'ddd'] 

我該怎麼辦呢?

+4

使用負先行斷言: '1(?!C)'*(1後面沒有C)* –

+1

're.split( 「1(?!三)」, 「aaa1bbb1ccc1ddd」)' – ozgur

回答

8

使用負先行用正則表達式和re模塊:

>>> string = 'aaa1bbb1ccc1ddd' 
>>> import re 
>>> re.split(r"1(?!c)", string) 
['aaa', 'bbb1ccc', 'ddd'] 
3
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') 
0

雖然沒有漂亮的正則表達式,我下面的代碼返回相同的結果:

string = 'aaa1bbb1ccc1ddd' 

分割字符串在所有情況下'1'

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']

+0

這個迭代器遍歷兩次(一次做'string.split ('1')'並且一次處理'p1')並且具有兩倍的存儲器佔用空間。正則表達式是更好的方式來做到這一點。 –

+0

謝謝@MatthewCole,這很有意義。學習正則表達式真的很有用! –

相關問題