Search and get a line in Python

前端 未结 4 1258
暖寄归人
暖寄归人 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:17

    With regular expressions

    import re
    s="""
        qwertyuiop
        asdfghjkl
    
        zxcvbnm
        token qwerty
    
        asdfghjklñ
    """
    >>> items=re.findall("token.*$",s,re.MULTILINE)
    >>> for x in items:
    ...     print x
    ...
    token qwerty
    

提交回复
热议问题