The code below is looking to highlight 1.2 or 1.4 when either number is found at the beginning of a line(^), is preceded by a word break (\b), underline (_) or some form of punctuation (\p{P}), likewise the number can be terminated by a word break (\b), underline (_) or some form of punctuation (\p{P}) or the end of the text.
- Code: Select all
public void RegexWithNonCaptureGroup()
{
string strFileName = @"RegexWithNonGroupTest.pdf";
PdfDocument pdfSource = new PdfDocument(strFileName);
// Find 1.4 or 1.2 either at the beginning or end of a line or when surrounded by punctuation
string strRegex = @"(?:\b|_|\p{P}|^)(1\.4)(?:\b|_|\p{P}|$)|(?:\b|_|\p{P}|^)(1\.2)(?:\b|_|\p{P}|$)";
PdfTextFind[] finds = pdfSource.Pages[0].FindText(strRegex, TextFindParameter.Regex).Finds;
if (finds.Length > 0)
{
foreach (PdfTextFind textCurrent in finds)
{
textCurrent.ApplyHighLight(Color.Gold);
}
}
pdfSource.SaveToFile(strFileName.Replace(".pdf", "-highlight.pdf"));
pdfSource.Dispose();
}
Unfortunately what happens is that values like (1.2 and "1.2 are highlighted rather than just the number as shown in the screenshot of the example document provided:
The expected behaviour was to highlight just the numbers, not the preceding punctuation or members of the non-capture group.
Please let me know if you have any questions,
Darren