Finding words after keyword in python

2019-03-12 10:35发布

I want to find words that appear after a keyword (specified and searched by me) and print out the result. I know that i am suppose to use regex to do it, and i tried it out too, like this:

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

The output is just:

"is"

But I want to get all the words and punctuations that comes after the word "name".

9条回答
神经病院院长
2楼-- · 2019-03-12 11:03

What you have used regarding your output:

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

What you have to use (match all):

re.search("name (.*)", s)
查看更多
我只想做你的唯一
3楼-- · 2019-03-12 11:07
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)
查看更多
相关推荐>>
4楼-- · 2019-03-12 11:09

Your example will not work, but as I understand the idea:

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"

This will print all after "name" and till end of the line.

查看更多
叼着烟拽天下
5楼-- · 2019-03-12 11:10

You could simply do

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

This will split your string and return a list like this ['hi my', 'is ryan, and i am new to python and would like to learn more']

depending on what you want to do this may help or not.

查看更多
一夜七次
6楼-- · 2019-03-12 11:18

Without using regex, you can

  • strip punctuation (consider making everything single case, including search term)

  • split your text into individual words

  • find index of searched word

  • get word from array (index + 1 for word after, index - 1 for word before )

Code snippet:

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

For multiple occurences, you need to save multiple indices:

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']
查看更多
做个烂人
7楼-- · 2019-03-12 11:20

Instead of "^name: (\w+)" use:

"^name:(.*)"
查看更多
登录 后发表回答