Home > OS >  Python regex: findall with parentheses inside
Python regex: findall with parentheses inside

Time:08-30

x="From Daniel: [email protected] UTC-8"
y=re.findall("^From (\S @\S )",x)

I want the output to be:

[[email protected]]

but it gives me an empty list, I really need the line to start with "From".

[]

CodePudding user response:

With your shown samples and attempts, please try following python3 code. Using Python's re module here and its findall function.

Here is the Online Demo for used regex.

import re
x="From Daniel: [email protected] UTC-8"
re.findall(r'^From\s .*?:\s(\S @\S )\s UTC-\d $',x)

['[email protected]']
  • Related