Search and get a line in Python

前端 未结 4 1296
暖寄归人
暖寄归人 2021-02-05 03:26

Is there a way to search, from a string, a line containing another string and retrieve the entire line?

For example:

string = 
    qwertyuiop
    asdfghj         


        
4条回答
  •  遇见更好的自我
    2021-02-05 04:28

    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='''
    qwertyuiop
    asdfghjkl
    
    zxcvbnm
    token qwerty
    
    asdfghjklñ
    '''
    

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

    ....
    ....
    token qwerty
    

提交回复
热议问题