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

Answer by UltraInstinct for Python extract pattern matches

$
0
0

You need to capture from regex. search for the pattern, if found, retrieve the string using group(index). Assuming valid checks are performed:

>>> p = re.compile("name (.*) is valid")>>> result = p.search(s)>>> result<_sre.SRE_Match object at 0x10555e738>>>> result.group(1)     # group(1) will return the 1st capture (stuff within the brackets).                        # group(0) will returned the entire matched text.'my_user_name'

Viewing all articles
Browse latest Browse all 12

Trending Articles