In .NET, you may make use of RegexOptions.RightToLeft
option to parse text from the end to its beginning, thus, getting last match much quicker and with simpler patterns.
Use
var text = " 26\r\nData related to the point SP-WFI-21-Room process fluids \r\nSampling Date:16/04/2007 \r\n 28\r\nData related to pint SP-WFI-21-Room process fluids \r\nSampling Date: 20/04/2007 \r\nTEST SPECIFICATIONS RESULTS \r\n 29\r\n3.2.P.4.2 Analytical Procedures \r\nAll the analytical procedures \r\n3.2.P.4.3 Validation of Analytical Procedures \r\nAll the analytical procedures proposed to control the excipients are those reported in Ph. Eur. \r\n− 3AQ13A: Validation of Analytical Procedures: Methodology - EUDRALEX Volume 3A \r\n3.2.P.4.4. Justification of Specifications";
var pattern = @"^\s*\d+\s*[\r\n]+(.*?)3\.2\.P\.4\.4\.\s+Justification\s+of\s+Specifications";
var regEx = new Regex(pattern, RegexOptions.RightToLeft | RegexOptions.Singleline | RegexOptions.Multiline );
var m = regEx.Match(text);
if (m.Success)
{
Console.WriteLine(m.Groups[1].Value);
}
See the C# demo.
See the .NET regex demo
I basically just added ^
(in multiline mode, start of a line) and \s*
after \d+
(just in case there are any spaces before the line break). Note the escaped dots.
Note that .NET regex does not support U
greediness switching modifier, thus the +?
must be turned to +
and .*
into .*?
. Actually, there were +
quantifiers that were meant to be +?
in the original regex, which might have led to other errors or unexpected behavior. Do not use U
modifier in PCRE if you are not 100% sure what you are doing.