比方說,我有這樣的文字:拆分並保持deliminator,最好用正則表達式
1.1 This is the 2,1 first 1.2 This is the 2,2 second 1.3 This is the 2,3 third
,我想:
["1.1 This is the 2,1 first","1.2 This is the 2,2 second","1.3 This is the 2,3 third"]
需要注意的是:
我不能使用
re.findall
,因爲我無法想出一種方法來正確終止比賽。我能想到的最好的是'[0-9]+\.[0-9]+^([0-9]+\.[0-9]+)*'
,這是行不通的。我不能將分隔符存儲爲全局變量,因爲它隨每個匹配而變化。
我無法使用常規的
re.split
,因爲我想保留分隔符。我不能使用逆序,因爲它必須是固定的寬度,而事實並非如此。
我已閱讀regexp split and keep the seperator,Python split() without removing the delimiter和In Python, how do I split a string and keep the separators?,仍然沒有答案。
但你*不*一定要小心你分裂的分隔符(空格)。 – jonrsharpe