简体   繁体   中英

Regex in Python to get the subject text from an email

I have multiple emails as text files and I have to extract the email's subject using regex

Subject: Meet the new Customer Support Representative

Dear team,

I am pleased to introduce you to [Name] who is starting today as a Customer Support Representative. She will be providing technical support and assistance to our users, ensuring they enjoy the best experience with our products.

Feel free to greet [Name] in person and congratulate her on the new role!

Best regards, [Your name] [Job title]

To extract the subject line from this sample email text, I tried using this regex: r"(?m)^Subject: (.+)$"
The output I got is:

Meet the new Customer Support Representative

But the output that I require is:

Subject: Meet the new Customer Support Representative

How can I modify this regex to get the "Subject:" part too?

You should put 'Subject: ' into the second capture group like r"(?m)^(Subject: .+)$" and use this value.

pattern = re.compile(r"(?m)^(Subject: .+)$")
match = pattern.match(text)
if match:
    result = match.group(2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM