Search and get a line in Python Search and get a line in Python python python

Search and get a line in Python


you mentioned "entire line" , so i assumed mystring is the entire line.

if "token" in mystring:    print(mystring)

however if you want to just get "token qwerty",

>>> mystring="""...     qwertyuiop...     asdfghjkl......     zxcvbnm...     token qwerty......     asdfghjklñ... """>>> for item in mystring.split("\n"):...  if "token" in item:...     print (item.strip())...token qwerty


If you prefer a one-liner:

matched_lines = [line for line in my_string.split('\n') if "substring" in line]


items=re.findall("token.*$",s,re.MULTILINE)>>> for x in items:

you can also get the line if there are other characters before token

items=re.findall("^.*token.*$",s,re.MULTILINE)

The above works like grep token on unix and keyword 'in' or .contains in python and C#

s='''qwertyuiopasdfghjklzxcvbnmtoken qwertyasdfghjklñ'''

http://pythex.org/matches the following 2 lines

........token qwerty