2017-02-12 130 views
-2

我想在文本這些短語找到的一部分。(我沒有想法如何使它)Python的正則表達式查找單詞的文字

  1. -is-和短語 - 到 - 這在大的文本,或者說是和TO

愛是-beatiful到你

愛IS-beatiful到你

loveISbeatifulTOyou

例如

「太陽是黑色的,但愛IS-beatiful到你,當你的微笑」

輸出

愛IS- beatiful-TO-you

  1. 如果華陽文字句子,我想整個句子

例如

睜開你的眼睛和愛。 come.fastFORyou立即

我想獲得(你可以看到它可以是點和空格)

愛。 come.fastFORyou

+0

*我想整個句子* - '愛。 come.fastFORyou' - 看起來不像一句話。它看起來像是兩句話的一部分。我想只會得到'come.fastFORyou'會更合理 – RomanPerekhrest

回答

1

考慮使用re.findall()功能與特定的正則表達式如下解決方案:

import re 

s = " The sun is black but love-IS-beatiful-TO-you when you smile. Open your eyes and love. come.fastFORyou immediately" 
result = re.findall(r'(\b\w+-?is-?\w+-?to-?\w+\b|(?:\w+(?:\.|\.)?)+foryou\b)', s, re.I) 

print(result) 

輸出:

['love-IS-beatiful-TO-you', 'love. come.fastFORyou'] 
相關問題