Quantcast
Channel: Python extract pattern matches - Stack Overflow
Viewing all articles
Browse latest Browse all 12

Answer by Ryan Stefan for Python extract pattern matches

$
0
0

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

Viewing all articles
Browse latest Browse all 12

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>