Finding words after keyword in python Finding words after keyword in python python python

Finding words after keyword in python


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.


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.


An other alternative...

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