Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Feb 24, 2022 1:28 pm

Hi, there seems to be an issue with using FindText with regex text that contains a non-capture group.

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:
Screenshot.jpg


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

wraydc
 
Posts: 130
Joined: Wed Apr 11, 2018 5:14 am

Fri Feb 25, 2022 3:58 am

Hello,

Thanks for your inquiry!

I did reproduce the highlight text issue according to your code and file. But after further investigate, I found that even just regex the string by C# with your pattern, the "(" and """will be matched (like the following screenshot). Our Spire.PDF is following the C# rules, so the result is the same as C#.

regexByC#.png


According to this article about regex: https://www.regular-expressions.info/unicode.html, the "\p{P}" in your regex pattern will match any kind of punctuation character, and this leads to the characters you do not want in the expect result.

p{P}explain.png

You can consider removing the "\p{P}" in your regex pattern like @"(?:\b|_|^)(1\.4)(?:\b|_|\p{P}|$)|(?:\b|_|^)(1\.2)(?:\b|_|\p{P}|$)". Or change the regex pattern until the matched text is in your expect. Hope you can understand.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Fri Feb 25, 2022 10:03 am

Thanks for your prompt investigation.

You are right if you just take the matches from the regex it will find the match for the entire regex - However in the case with non-capture groups, you would usually use the Groups rather than just taking the Match values directly.

What is the best way to achieve the desired outcome (i.e. highlighting just 1.2 or 1.4 in the example) using Spire.PDF?

Thanks in advance,

Darren.

wraydc
 
Posts: 130
Joined: Wed Apr 11, 2018 5:14 am

Mon Feb 28, 2022 9:38 am

Hello Darren,

Thanks for your feedback!

As I posted before, we followed the regex results which under C#. The only method to match only 1.2 or 1.4 as you wish is changing your regex pattern, such as remove the "\p{P}" before the number like @"(?:\b|_|^)(1\.4)(?:\b|_|\p{P}|$)|(?:\b|_|^)(1\.2)(?:\b|_|\p{P}|$)" (do not match any punctuation). Or you need to know that which form of punctuations you need and use the corresponding code like "\p{Pd}" .etc according to the described in this article[url]:https://www.regular-expressions.info/unicode.html[/url].

Hope you can understand.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Mon Feb 28, 2022 10:41 am

Thanks for the quick response.

Unfortunately even using the methods suggested will cause "over highlighting" by including any characters from the non-capture group when there's a match. Perhaps a future enhancement could include more granular access equivalent to accessing the groups.

In the meantime, I'll have to see if I can find a workaround for this.

wraydc
 
Posts: 130
Joined: Wed Apr 11, 2018 5:14 am

Tue Mar 01, 2022 6:31 am

Hello Darren,

Thanks for your feedback!

I tested with the following code as I mentioned before with the latest Spire.Pdf version 8.2.2, and the result file only highlight the "1.2" and "1.4" as you wish.
Code: Select all
        public static 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|_|^)(1\.4)(?:\b|_|\p{P}|$)|(?:\b|_|^)(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();
            Process.Start(strFileName.Replace(".pdf", "-highlight.pdf"));
        }

result.png

If you were not using the latest version, I just recommend you have a try. If the issue still exists, please provide us with your system information (E.g. Win7, 64 bit) and region setting (E.g. China, Chinese). Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.PDF