Python extract pattern matches

前端 未结 9 1498
小蘑菇
小蘑菇 2020-11-22 06:36

Python 2.7.1 I am trying to use python regular expression to extract words inside of a pattern

I have some string that looks like this

someline abc
s         


        
9条回答
  •  一向
    一向 (楼主)
    2020-11-22 07:29

    Maybe that's a bit shorter and easier to understand:

    import re
    text = '... someline abc... someother line... name my_user_name is valid.. some more lines'
    >>> re.search('name (.*) is valid', text).group(1)
    'my_user_name'
    

提交回复
热议问题