9

Pythonで文章中の「s」で始まる単語を整理したい。
これが私のコードです:

import re
text = "I was searching my source to make a big desk yesterday."
m = re.findall(r'[s]\w+', text)
print m

しかし、コードの結果は次のとおりです。

['searching', 'source', 'sk', 'sterday'].

正規表現に関するコードを書くにはどうすればよいですか? または、言葉を整理する方法はありますか?

4

6 に答える 6

20
>>> import re
>>> text = "I was searching my source to make a big desk yesterday."
>>> re.findall(r'\bs\w+', text)
['searching', 'source']

小文字と大文字のs使用:r'\b[sS]\w+'

于 2013-05-08T12:07:55.357 に答える
11

正規表現ソリューションではないことは知っていますが、使用できますstartswith

>>> text="I was searching my source to make a big desk yesterday."
>>> [ t for t in text.split() if t.startswith('s') ]
['searching', 'source']
于 2013-05-08T12:46:25.410 に答える