Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中查找关键字后的单词_Python_Regex_Matching_Keyword - Fatal编程技术网

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

在python中查找关键字后的单词,python,regex,matching,keyword,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() mystring = "hi my name is ryan, and i am new to

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

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()
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'
输出仅为:

"is"
但是我想获得单词“name”后面的所有单词和标点符号。

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

"^name:(.*)"

不使用正则表达式,您可以(例如)用如下方式分隔字符串:

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()
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'

但是,您必须单独处理不必要的空白。

关于输出,您使用了什么:

re.search("name (\w+)", s)
必须使用的内容(全部匹配):

你可以这么做

s = "hi my name is ryan, and i am new to python and would like to learn more"
s.split('name')
这将拆分字符串并返回如下列表['hi my','is ryan,我是python新手,希望了解更多信息']


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

你的例子不起作用,但据我所知:

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"

这将在“name”之后一直打印到行尾。

另一种选择

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

m=re.search(“(?这将适用于u:work name\s\w+\s(\w+)


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

  • 带标点符号(考虑将所有内容都设置为单个大小写,包括搜索词)

  • 将文本拆分为单个单词

  • 查找搜索词的索引

  • 从数组中获取单词(
    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']

如果在同一个句子中有多个“name”怎么办?你的解决方案将不起作用,它应该是
mystring.partition(keyword)
如果
keyword
周围没有引号,那么如果
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.group(1)