Hello. Suppose there is a text in the text variable STR:

1212 FDFER5678 golFINDME1244fwfw fewf 3434 

Required by the substring "FINDME", from the text find its location in the text and cut the entire line in which it is contained, from \ n to \ n.

The required output of the code will be "golFINDME1244fwfw".

It is advisable to use python 2.

  • What did you try to do and why did you fail? - andreymal

3 answers 3

 STR = """1212 FDFER5678 golFINDME1244fwfw fewf 3434""" lst = STR.split('\n') for line in lst: if "FINDME" in line: print line 
     import re Findall = """1212 FDFER5678 golFINDME1244fwfw fewf 3434""" mother = Findall.split() for word in mother: if re.search('FINDME', word): print (word) 
    • in the question ask "from \ n to \ n." find, not from space to space. - jfs

    To find the first line containing the given substring:

     >>> import re >>> re.search(r'^.*FINDME.*$', STR, re.MULTILINE).group() 'golFINDME1244fwfw' 

    To find all such lines:

     >>> re.findall('^.*FINDME.*$', STR, re.MULTILINE) ['golFINDME1244fwfw']