C# Regex dates from text

Viewed 47

I have the following text from a file that I need to find the two values (60 & 1),two amounts and the two dates. The sample text is:

Amount of Payments 
 
 
 
60 
(e) 
 
 
 
$ 1160.87  (e) 
Monthly, Starting 
               2022-07-08 00:00:00              (e)            
 
  
1 
(e) 
 
 
$ 895.85  (e) 
 
                2027-07-08 00:00:00              (e) 

So far I have the following Regex pattern to pull the first value, first amount and first date, but I am not sure how to go about getting the rest of the information.

string pattern = @"Amount\sof\sPayments[\n|\s]+(?'term'[0-9]{1,3})[\n|\s]+\(e\)[\n|\s]+\$\s(?'payment_amt'[+-]?[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2})[\n|\s]+\(e\)[\n|\s]+Monthly\,\sStarting[\n|\s]+(?'start_date'\d{4}\-\d{1,2}\-\d{1,2})";
1 Answers

Perhaps it is enough to make sure the (e) parts and the amount format is present with 3 capture groups, where \s can also match newlines:

\b(?'term'[0-9]+)\s*\(e\)\s*\$\s*(?'payment_amt'[0-9]+(?:\.[0-9]+)?)\s*\(e\)[\S\s]*?\b(?'start_date'\d{4}-\d{1,2}-\d{1,2})\b

Regex demo

If the first line should be present, you can use a positive lookbehind in C# with an infinite quantifier.

To take the newlines into account, you can enable multiline.

(?ms)(?<=^Amount\sof\sPayments\b.*)(?'term'[0-9]+)\r?\n\s*\(e\)\r?\n\s*^\$\s*(?'payment_amt'[0-9]+(?:\.[0-9]+)?)\s*\(e\).*?\r?\n[^\S\r\n]+(?'start_date'\d{4}-\d{1,2}-\d{1,2})\b

Regex demo

Related