2015-11-08 27 views
-3

我想統計工廠在文本文件中出現的次數。然而,一些植物的情況下,顯示爲:如何拆分python中的連接詞

Floweredplants or plantdaisy. 

我怎麼可以拆分上面的字,用「植物」使用正則表達式

回答

0

替換它們你可以使用一些簡單的正則表達式來匹配您的所有可能的情況下,像這樣:

import re 
re.search('[A-Za-z]*plants?', 'floweredplants') 
1

你可以這樣做:

if s.find("plant") >= 0: 
    print "It contains plant" 

這只是測試,如果 「植物」 我s的子字符串sfind返回-1如果參數不存在於字符串中)。那是你在找什麼?

1

使用界限:

\bplant\w*   # will match 'plantation' 
\w*plant\b   # will match 'eleplant' 
\w*plant\w*  # will match any of previous examples and 'eleplanted' 
\bplant\b   # will match only exactly 'plant' words 

希望它幫助。