Bug in RegEx replace function

SyntaxEditor for Windows Forms Forum

Posted 9 years ago by Michael Dempsey - Sr. Developer, Teradata Corp
Version: 14.1.0321
Platform: .NET 4.5
Environment: Windows 8 (64-bit)
Avatar

One of my customers just reported a strange problem with the RegEx replace function.

A simplified version of the problem is:

Search String:              (.*)
Replace expression:    $1xxx$1
Replace All

Input        Expected Result     Actual Result

c1             c1xxxc1                 c1xxxc1xxx
c2             c2xxxc2                 c2xxxc2xxx
c3             c3xxxc3                 c3xxxc3

If we change the Search string to (.+) it works as expected.
Or we can use (\w+) also... but not (\w*).

Is the '*' somehow matching the LineEnd characters also?
But if so, that should cause it to add additional line end characters, so that can't be it.

Note that this is a very simplified version of what the customer is actually trying to do.

Comments (2)

Answer - Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Mike,

I believe what's happening is that your regex pattern allows effectively "nothing" to get consumed in some cases where there is no match.  For instance if you have the text "c1 c2" and you search on "(\w*)", you will get three results:

  • 0-2 ("c1")
  • 2-2 ("" - empty string)
  • 3-5 ("c2")

It's those empty string matches that end up adding those extra "xxx".  VS seems to do the same thing.  I would recommend using a better search pattern that requires at least one character to be consumed.


Actipro Software Support

Posted 9 years ago by Michael Dempsey - Sr. Developer, Teradata Corp
Avatar

OK thanks.

I thought their might be null matches.

I had already suggested different search strings that work for the customer but i just wanted to report it in case this was a bug that needed to be fixed.

The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.