Regex pattern in between texts

Hello,

I have the below sample strings where I need to extract
Dec. 28 Dec. 29 USD97.46@1.319823517 DESIGNS FOR HEALTH 8008478302 CT 014000576995 128.63
Dec. 30 Dec. 31 USD1,667.01@1.314755160 EconomyBookings.com 1 202 800 993LVA 000046645013 2,191.71
Dec. 30 Dec. 31 MANULIFE TRAVEL INSURA TORONTO ON 800175645626 112.12 CR

everything after the second date but before the reference
USD97.46@1.319823517 DESIGNS FOR HEALTH 8008478302 CT in line1
USD1,667.01@1.314755160 EconomyBookings.com 1 202 800 993LVA in line2
MANULIFE TRAVEL INSURA TORONTO ON in line3

can someone please help?

Hi @lynnsong986

Try this expression

System.Text.RegularExpressions.Regex.Match(InputString,"(?s)(?<=\S{3}.\s\d{2}\s\S{3}.\s\d{2}\s).*?(?=\d{12})").ToString

Regards
Gokul

Thanks so much Gokul, however some lines aren’t returning

Hello,

Try this

(?<=\S{3}\.\s\d{1}\s\S{3}\.\s\d{1}\s).*?(?=\.*\d{12})|(?<=\S{3}\.\s\d{1}\s\S{3}\.\s\d{2}\s).*?(?=\.*\d{12})|(?<=\S{3}\.\s\d{2}\s\S{3}\.\s\d{1}\s).*?(?=\.*\d{12})|(?<=\S{3}\.\s\d{2}\s\S{3}\.\s\d{2}\s).*?(?=\.*\d{12})

Regards
Subham Don

just modified Gokul’s answer a bit

Hi,

FYI, the following expression might be better.

mc = System.Text.RegularExpressions.Regex.Matches(yourString,"(?<=^[A-Za-z]{3}\.\s+\d{1,2}\s+[A-Za-z]{3}\.\s+\d{1,2}\s+).*(?=\d{12})",System.Text.RegularExpressions.RegexOptions.Multiline)

note: mc is MatchCollection type

Regards,

3 Likes

Hi @lynnsong986

You can try with OR | operator in the regex expression

System.Text.RegularExpressions.Regex.Match(InputString,"(?s)(?<=\S{3}.\s\d{2}\s\S{3}.\s\d{2}\s).*?(?=\d{12})|(?s)(?<=\S{3}.\s\d{2}\s\S{3}.\s\d{1}\s).*?(?=\d{12})|(?s)(?<=\S{3}.\s\d{1}\s\S{3}.\s\d{1}\s).*?(?=\d{12})|(?s)(?<=\S{3}.\s\d{1}\s\S{3}.\s\d{2}\s).*?(?=\d{12})").ToString

image

Regards
Gokul

1 Like

Thank you all! it works well now!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.