我想整理出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
Run Code Online (Sandbox Code Playgroud)
但代码的结果是:
['searching', 'source', 'sk', 'sterday'].
Run Code Online (Sandbox Code Playgroud)
如何编写有关正则表达式的代码?或者,有什么方法可以整理单词吗?
jam*_*lak 12
>>> import re
>>> text = "I was searching my source to make a big desk yesterday."
>>> re.findall(r'\bs\w+', text)
['searching', 'source']
Run Code Online (Sandbox Code Playgroud)
对于小写和大写s使用:r'\b[sS]\w+'
我知道它不是正则表达式解决方案,但你可以使用 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']
>>>
Run Code Online (Sandbox Code Playgroud)