在python中查找关键字后面的单词

时间:2011-07-09 08:30:57

标签: python regex matching keyword

我想找到关键字后面出现的单词(由我指定和搜索)并打印出结果。我知道我想使用正则表达式来做这件事,我也试过了,就像这样:

import re
s = "hi my name is ryan, and i am new to python and would like to learn more"
m = re.search("^name: (\w+)", s)
print m.groups()

输出只是:

"is"

但是我想得到“名字”这个词之后的所有单词和标点符号。

9 个答案:

答案 0 :(得分:27)

您可以(例如)将您的字符串与str.partition(separator)分开,而不是使用正则表达式:

mystring =  "hi my name is ryan, and i am new to python and would like to learn more"
keyword = 'name'
before_keyword, keyword, after_keyword = mystring.partition(keyword)
>>> before_keyword
'hi my '
>>> keyword
'name'
>>> after_keyword
' is ryan, and i am new to python and would like to learn more'

但是你必须分别处理不必要的空格。

答案 1 :(得分:10)

你的例子不起作用,但我理解这个想法:

regexp = re.compile("name(.*)$")
print regexp.search(s).group(1)
# prints " is ryan, and i am new to python and would like to learn more"

这将打印所有“名称”后直到行的结尾。

答案 2 :(得分:4)

另一种选择......

   import re
   m = re.search('(?<=name)(.*)', s)
   print m.groups()

答案 3 :(得分:3)

而不是"^name: (\w+)"使用:

"^name:(.*)"

答案 4 :(得分:2)

您对输出使用了什么:

re.search("name (\w+)", s)

你必须使用什么(匹配所有):

re.search("name (.*)", s)

答案 5 :(得分:1)

你可以简单地做

s = "hi my name is ryan, and i am new to python and would like to learn more"
s.split('name')

这会拆分你的字符串并返回一个像这样的列表['hi my','是ryan,我是python的新手,想了解更多']

取决于你想做什么,这可能有所帮助。

答案 6 :(得分:1)

这将适用于你:工作名称\ s \ w + \ s(\ w +)

>>> s = 'hi my name is ryan, and i am new to python and would like to learn more'
>>> m = re.search('name\s\w+\s(\w+)',s)
>>> m.group(0)
'name is ryan'
>>>> m.group(1)
'ryan'

答案 7 :(得分:1)

import re
s = "hi my name is ryan, and i am new to python and would like to learn more"
m = re.search("^name: (\w+)", s)

print m.group(1)

答案 8 :(得分:0)

不使用正则表达式,你可以

  • 剥离标点符号(考虑将所有内容都设为单个案例,包括搜索词)

  • 将您的文字拆分为单个单词

  • 查找搜索词的索引

  • 从数组中获取单词(index + 1后面的单词,index - 1之前的单词)

代码段:

import string
s = 'hi my name is ryan, and i am new to python and would like to learn more'
t = 'name'
i = s.translate(string.maketrans("",""), string.punctuation).split().index(t)
print s.split()[i+1]

>> is

对于多次出现,您需要保存多个索引:

import string
s = 'hi my NAME is ryan, and i am new to NAME python and would like to learn more'
t = 'NAME'
il = [i for i, x in enumerate(s.translate(string.maketrans("",""), string.punctuation).split()) if x == t]
print [s.split()[x+1] for x in il]

>> ['is', 'python']
相关问题