Grabbing a part of a string using regex in python 3.x -
so trying have input field named a. have line of regex checks 'i (something)' (note chain of words.) , prints how long have been (something)?
this code far:
if re.findall(r"i am", a): print('how long have been {}'.format(re.findall(r"i am", a)))
but returns me list of [i, am] not (something). how return me (something?)
thanks,
a n00b @ python
do mean this?
>>> import re >>> = "i programmer" >>> reg = re.compile(r'i (.*?)$') >>> print('how long have been {}'.format(*reg.findall(a))) how long have been programmer
r'i (.*?)$'
matches i am
, else end of string.
to match 1 word after, can do:
>>> = "i apple" >>> reg = re.compile(r'i (\w+).*?$') >>> print('how long have been {}'.format(*reg.findall(a))) how long have been
Comments
Post a Comment