-3

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?

1 Answers1

0

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)
clemens
  • 16,716
  • 11
  • 50
  • 65