You can also use a capture group (?P<user>pattern)
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<user>.*) is valid'matches = re.search(pattern, str(string), re.DOTALL)print(matches['user'])# my_user_name