Bug in RegEx Engine (negative look behind)

SyntaxEditor for Windows Forms Forum

Posted 15 years ago by Joachim Schwieren
Avatar
Hi,

I' trying to use the search & replace feature of SyntaxEditor to implement an intelligent "quote-in"/"quote-out" functionality: e.g.

If the text is "this is a sample" it shall become \" this is a sample \".

This is quite simple, but if the text is "This is a \"good\" example" it shall become \"This is a "\good\" example\".

So the idea is to prevent a double quote.

Here is my approach:
FindReplaceOptions opts = new FindReplaceOptions();
opts.FindText = "(?<!\\\\)\\\"";
opts.ReplaceText = "\\\"";
opts.SearchType = FindReplaceSearchType.RegularExpression;
Unfortunately the negative look behind does not work (Exception text: "Op code not implemented: 0"). In contrast to that, a positive look ahead works well but does not help in the scenario...

Why doesn't this work?

Thanks,

Joe

[Modified at 08/03/2009 10:19 AM]

Comments (5)

Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Joe,

Thanks for letting us know. We've found the bug and have it fixed for the next maintenance release.


Actipro Software Support

Posted 15 years ago by Joachim Schwieren
Avatar
Hi,

the bug seems to be solved but I think I found another one (or made a mistake):

Here ist my code:

            FindReplaceOptions opts = new FindReplaceOptions();
            opts.FindText = "(?<!\\\\)\\\"";
            opts.ReplaceText = "\\\"";
            opts.SearchType = FindReplaceSearchType.RegularExpression;
            opts.SearchInSelection = (!SyntaxEditor.SelectedView.Selection.IsZeroLength);
            FindReplaceResultSet result = SyntaxEditor.SelectedView.FindReplace.ReplaceAll(opts);
The idea is to replace all " with \" but not the \" in the text.

The ResultSet that is returned displays the the correct number of replacements performed but there are no changes visible in the text.

If I change opts.ReplaceText = "\\\""; to opts.ReplaceText = "x"; it works, but of course I do not want to replace with x... it must be a problem with the chars \".

Do you have an idea?

Thanks,

Joachim Schwieren
Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Joachim,

If I run our demo, and enter these values in the find/replace dialog:
Find what: (?<!\\)\"
Replace with: \\"

I see replacements work. Remember that you need to do a regex escape on the backslash in replace text because it has special meaning. So you probably need two more backslashes at the start of your C# regex replace text.


Actipro Software Support

Posted 15 years ago by Joachim Schwieren
Avatar
Hi,

the two extra \\ did it - thanks! But somehow strange; why does also the replace string has to be regex quoted? I thought that only the search string has to be like that since it represents the regex pattern to match... anyway it works now!

Regards,

Joe
Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Joe,

After looking into it a bit more, we found that you are right. The regex replacement pattern shouldn't be using the backslash as an escape. The only special character in a replacement pattern is $, which can be used for substitutions and can be escaped by $$. We've updated our code for the next build to remove backslash as an escape for regex replacement patterns. Thanks for the note.


Actipro Software Support

The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.