1

I'm working on extracting specif string set after matching a pattern but results are not as expected. Instead of first occurrence starting from top of the text file the function picks very last occurrence.

Function:

[\n\r].*Sent:\s*([^\n\r]*)

Sample text:

From: Y Sent: Monday, November 6, 2018 6:38 AM To: X

BLA BLA

Thank you,

From: X Sent: Monday, November 5, 2018 8:38 AM To: Y

Hi Y BLA

Thanks,

Expected results:

Monday, November 6, 2018 6:38 AM

Curently returns:

 Monday, November 5, 2018 8:38 AM
marcin2x4
  • 619
  • 3
  • 13

2 Answers2

2

The first occurrence is not being matched because you start your regex with [\n\r] which matches a newline and is not present before the first line in your example data.

To get your matches, you can omit [\n\r].* from the beginning and add To: at the end. If you don't use the global flag you will get only the first occurence and your match is in the first capturing group.

Sent:\s*([^\n\r]*) To:

Regex demo

The fourth bird
  • 96,715
  • 14
  • 35
  • 52
1

You're close. Try this:

Sent:\s?(.*?)\sTo:

This looks for 'Sent', a colon, an optional White Space, then it creates Group 1, matching any number of any char until it reaches a White Space and 'To:'.

If you set the global flag, it will match both dates, otherwise just the first.

The date will be in Group 1.

Poul Bak
  • 7,390
  • 4
  • 20
  • 40