我有一組要回圈的字串,它們都是不同的,但可以使用相同的關鍵字進行分解。這將提取所有以相同單詞開始和結束但具有不同值的子字串。采取以下字串:
res = "But also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop"
要提取字串:"But also the leap into electronic" 我可以使用:
first_line = re.findall('But.*electronic', res)
但是“電子”一詞在每個字串中都會發生變化,而“排版”將保持不變。如何提取以“但是”開頭并以“排版”之前的單詞結尾的文本?
uj5u.com熱心網友回復:
您可以使用先行斷言:
re.findall(r'\bBut.*(?=\stypesetting\b)', res)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/533743.html
標籤:Python正则表达式
下一篇:在R中使用正則運算式識別模式