33

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".

Hooked
  • 84,485
  • 43
  • 192
  • 261
Ryan
  • 359
  • 2
  • 4
  • 4

9 Answers9

45

Instead of using regexes you could just (for example) separate your string with str.partition(separator) like this:

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'

You have to deal with the needless whitespaces separately, though.

Dan Russell
  • 960
  • 8
  • 22
Aufwind
  • 25,310
  • 38
  • 109
  • 154
20

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.

DominiCane
  • 1,263
  • 3
  • 16
  • 29
10

An other alternative...

   import re
   m = re.search('(?<=name)(.*)', s)
   print m.groups()
fransua
  • 1,559
  • 13
  • 30
4
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)
lebelinoz
  • 4,890
  • 10
  • 33
  • 56
krish
  • 41
  • 1
3

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

"^name:(.*)"
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
2

This will work out for u : work name\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'
Jerry
  • 966
  • 2
  • 13
  • 28
2

What you have used regarding your output:

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

What you have to use (match all):

re.search("name (.*)", s)
Tugrul Ates
  • 9,451
  • 2
  • 33
  • 59
2

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.

Parag Tyagi
  • 8,780
  • 3
  • 42
  • 47
Hassek
  • 8,715
  • 6
  • 47
  • 59
1

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']
philshem
  • 24,761
  • 8
  • 61
  • 127