Finding words after keyword in python

后端 未结 9 1872
清酒与你
清酒与你 2020-12-08 20:19

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

相关标签:
9条回答
  • 2020-12-08 21:07

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

    "^name:(.*)"
    
    0 讨论(0)
  • 2020-12-08 21:16

    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.

    0 讨论(0)
  • 2020-12-08 21:18

    An other alternative...

       import re
       m = re.search('(?<=name)(.*)', s)
       print m.groups()
    
    0 讨论(0)
提交回复
热议问题