BoldDesk®Customer service software offering ticketing, live chat, and omnichannel support, starting at $49/mo. for 10 agents. Try it for free.
I need to iterate over a single document several times to progressively find and replace content.
My first iteration looks for certain tags and replaces them. I am using document.FindSingleLine(regEx) and
document.FindNextSingleLine(nextParagraph, regEx).
On my second iteration I need to go back through the whole document again finding and replacing different items. However, the first result of the document.FindSingleLine(regEx) in the second iteration is not the first result in the document but the first result after the latest result of the first iteration.
For example, if my document was:
1
A
2
B
3
C
4
5
And my first iteration replaced all letters with @ I would have:
1
@
2
@
3
@
4
5
But when my second iteration replaces all numbers with # I get:
1
@
2
@
3
@
#
#
When I expect to get:
#
@
#
@
#
@
#
#
It seems like there may be an internal cursor or pointer that is not getting reset to the top of the document when FindSingleLine is executed.
Any ideas on how to resolve this?
I found a solution but it is not very clean if you ask me.
I can use:
var firstParagraph = document.Sections[0].Paragraphs[0]
to get the first paragraph in the document. And then I can use:
document.FindNextSingleLine(firstParagraph, regEx)
at the start of my iteration to ensure that the first find starts at the top of the document.
I would have expected document.FindSingleLine(regEx) to do this automatically.
Thanks Lokesh, but my example of simple letters and numbers was just to help explain. The actual template document has complex tags I need to find that can span multiple paragraphs. That is why I needed to use FindSingleLine. My problem is that FindSingleLine does not always seem to start at the top of the document.
I have updated your example with code similar to what I am using and it demonstrates the problem. I am attaching a new MainPage.aspx.cs file.
It produces this result:
1
@
2
@
3
@
#
#
To add to the weirdness, I inadvertently found that if I call "document.ReplaceSingleLine(new Regex(@"DUMMY TEXT"), "DUMMY TEXT");" before the Replace Numbers section of the code the problem is resolved. The only thing I can think of is that the ReplaceSingleLine must do some kind of reset that makes the next FindSingleLine call work as expected.
See updated MainPage.aspx.cs file attached.
I also found a new issue. I am posting it hear because it is somewhat related and I am using the example code you provided to demonstrate the issue.
This issue involves the ReplaceSingleLine injecting extra line returns.
I updated the template doc to look like this:
I have Word showing paragraph marks so it is clear where the line returns are.
My replace code looks like this:
The code should find "#~" followed by anything and then "~#" and replace it with "X". As you can see I have set my RegEx pattern to include line returns when it finds anything by adding the (?s) to the front of the pattern. The RegEx does indeed find the correct text. The problem is that it does not replace it properly.
I should be getting a result that looks like this:
But what I get is what you see below with the extra paragraph mark between the two Xs. Why is that extra paragraph mark there? How do I fix this problem?
Thanks for the update. Sorry I did not reply right away. Is it possible to add another email address to this thread for notifications? If so, please add andrew.hawes@claritymis.com. Thanks
I don't think I am using your "weekly NuGet version". I don't really know how I would use the weekly version.
As far as I know, I am using version 19.2.0.44.
I would like to test the fix when it comes out. How would I do that?
Thank you. I will review and get back with you.
Both issues seem to be addressed. At least they are in the sample project. I will also be testing in the real project to make sure. I will update you all in the next few days.