[英]Python Regex - Get Substring Starting with part of word and ending with certain character
[英]regex split at start of word ending in character
我再次嘗試找出如何在Python中分割字符串,該字符串具有以下類型的格式:
'aaaa bbbb cccc:dd eeee:ff ggg hhhh iiii:jjjj kkkk:llll:mm nnn:ooo pppp qqqq:rrr'
進入以下列表項:
'aaaa bbbb'
'cccc:dd'
'eeee:ff ggg hhhh'
'iiii:jjjj'
'kkkk:
'llll:mm'
'nnn:ooo pppp'
'qqqq:rrr'
我正在尋找以冒號(':')結尾的單詞的開頭
我們歡迎所有的建議 :)
提供的示例的以下作品:
import re
string = 'aaaa bbbb cccc:dd eeee:ff ggg hhhh iiii:jjjj kkkk:llll:mm nnn:ooo pppp qqqq:rrr'
result = []
# split the string at each word followed by a colon
# wrap regex pattern as group so it is added to result list
parts = re.split("(\w+:)", string)
# if anything was previous to first delimitation token
# add it to results
if parts[0]:
result.append(parts[0].strip())
# create pairs of a delimitation token and next string
# start from first delimitation token (list index 1)
groups = zip(*[parts[i+1::2] for i in range(2)])
# join each pair to one string and strip spacing
result.extend(["".join(group).strip() for group in groups])
print(result)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.