Python 如何查找以特定字符开头的单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16440267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find a word that starts with a specific character
提问by PrimingRyan
I want to sort out words which are started with 's' in sentence by python.
Here is my code:
我想通过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
But the result of code is :
但代码的结果是:
['searching', 'source', 'sk', 'sterday'].
How do I write a code about regular expression? Or, is there any method to sort out words?
如何编写有关正则表达式的代码?或者,有没有什么方法可以整理单词?
采纳答案by jamylak
>>> import re
>>> text = "I was searching my source to make a big desk yesterday."
>>> re.findall(r'\bs\w+', text)
['searching', 'source']
For lowercase and uppercase suse: r'\b[sS]\w+'
对于小写和大写s使用:r'\b[sS]\w+'
回答by stema
If you want to match a single character, you don't need to put it in a character class, so
sis the same than[s].What you want to find is a word boundary. A word boundary
\bis an anchor that matches on a change from a non word character (\W) to a word character (\w) or vice versa.
如果要匹配单个字符,则不需要将其放入字符类中,因此
s与[s].你要找的是一个词边界。单词边界
\b是匹配从非单词字符 (\W) 到单词字符 (\w)的变化的锚点,反之亦然。
The solution is:
解决办法是:
\bs\w+
this regex will match on a swith not a word character before (works also on the start of the string) and needs at least one word character after it. \w+is matching all word characters it can find, so no need for a \bat the end.
此正则表达式将匹配s前面没有单词字符的字符(也适用于字符串的开头),并且后面至少需要一个单词字符。\w+匹配它可以找到的所有单词字符,所以不需要\b在最后加上a 。
See it here on Regexr
在 Regexr 上看到它
回答by Adem ?zta?
I know it is not a regex solution, but you can use startswith
我知道这不是正则表达式解决方案,但您可以使用 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']
回答by S A G A R
I would like to add one small thing here,
我想在这里补充一点,
Let's say you have a line to find words which starts with 's'
假设您有一行查找以“s”开头的单词
line = "someone should show something to [email protected]"
line = "有人应该向 [email protected] 展示一些东西"
if you write regular expression like, swords = re.findall(r"\b[sS]\w+", line)
如果你写这样的正则表达式,swords = re.findall(r"\b[sS]\w+", line)
output will be, ['someone','should','show','something','some']
输出将是,['someone','should','show','something','some']
But if you modify regular expression to,
但是如果你修改正则表达式为,
use \S instead of \w
使用 \S 而不是 \w
swords = re.findall(r"\b[sS]\S+", line)
Swords = re.findall(r"\b[sS]\S+", line)
output will be, ['someone','should','show','something','[email protected]']
输出将是,['someone','should','show','something','[email protected]']
回答by Narekzzz
I tried this sample of code and I think it does exactly what you want:
我尝试了这个代码示例,我认为它完全符合您的要求:
import re
text = "I was searching my source to make a big desk yesterday."
m = re.findall (r'\b[s]\w+', text)
print (m)
回答by user3533685
Lambda style:
拉姆达风格:
text = 'I was searching my source to make a big desk yesterday.'
list(filter(lambda word: word[0]=='s', text.split()))
Output:
输出:
['searching', 'source']
['searching', 'source']

