如何查找以特定字符开头的单词 [英] How to find a word that starts with a specific character

查看:40
本文介绍了如何查找以特定字符开头的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用python整理句子中以's'开头的单词.
这是我的代码:

I want to sort out words which are started with 's' in sentence by python.
Here is my code:

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'].

关于正则表达式的代码怎么写?或者,有没有什么方法可以整理单词?

How do I write a code about regular expression? Or, is there any method to sort out words?

推荐答案

>>> 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+'

For lowercase and uppercase s use: r'\b[sS]\w+'

这篇关于如何查找以特定字符开头的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆