亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

正則表達式返回匹配加上字符串直到下一個匹配

正則表達式返回匹配加上字符串直到下一個匹配

慕俠2389804 2023-07-27 16:43:48
目標:根據數字或小數匹配將文本分解為列表,檢索直到但不包括下一個匹配的所有文本。語言/版本:使用 python re.findall() 的 Python 3.8.5,我愿意接受替代建議。文本示例(是的,全部在一行上): 1 Something Interesting here 2 More interesting text 2.1 An example of 2C19 a header 2.3 Another header example 2.4 another interesting header 10.1 header stuff  14 the last interesting 3A4 header目標輸出:['1 Something Interesting here','2 More interesting text','2.1 An example of 2C19 a header','2.3 Another header example','2.4 another interesting header','10.1 header stuff','14 the last interesting 3A4 header']我可以使用以下方法識別大多數適當的整數/小數起點:(\d+\.\d+)|([^a-zA-Z]\d\d)|( \d )我正在努力尋找一種方法來返回匹配項之間的文本以及匹配項本身。為了節省您一些時間,這是我的正則表達式沙箱
查看完整描述

1 回答

?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

您可以使用正向先行表達式來匹配,直到下一個匹配。


這是更新的正則表達式(沙箱):


\b(?:\d+(?:\.\d+)?)\b.*?(?=\b(?:\d+(?:\.\d+)?)\b|$)


在Python中:


regex = r'\b(?:\d+(?:\.\d+)?)\b.*?(?=\b(?:\d+(?:\.\d+)?)\b|$)'

string = ' 1 Something Interesting here 2 More interesting text 2.1 An example of 2C19 a header 2.3 Another header example 2.4 another interesting header 10.1 header stuff  14 the last interesting 3A4 header'

result = re.findall(regex, string)

在這種情況下,result將是:


>>> result

['1 Something Interesting here ',

 '2 More interesting text ',

 '2.1 An example of 2C19 a header ',

 '2.3 Another header example ',

 '2.4 another interesting header ',

 '10.1 header stuff  ',

 '14 the last interesting 3A4 header']

請注意,此解決方案還會提取末尾的間距。如果你不想要這個間距,你可以調用strip你的字符串:


>>> [ match.strip() for match in result ]

['1 Something Interesting here',

 '2 More interesting text',

 '2.1 An example of 2C19 a header',

 '2.3 Another header example',

 '2.4 another interesting header',

 '10.1 header stuff',

 '14 the last interesting 3A4 header']


查看完整回答
反對 回復 2023-07-27
  • 1 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號