Python extract pattern matches

前端 未结 9 1618
小蘑菇
小蘑菇 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:09

    You can also use a capture group (?Ppattern) and access the group like a dictionary match['user'].

    string = '''someline abc\n
                someother line\n
                name my_user_name is valid\n
                some more lines\n'''
    
    pattern = r'name (?P.*) is valid'
    matches = re.search(pattern, str(string), re.DOTALL)
    print(matches['user'])
    
    # my_user_name
    

提交回复
热议问题