ducoral
ducoral

Reputation: 9

Delphi TRegEx bug?

I try to validate the input '3a' for regex '[_a-zA-Z][_a-zA-Z0-9]*' with that source:

len := TRegEx.Create([_a-zA-Z][_a-zA-Z0-9]*).Match('3a').Length;

I expected 0 for len variable, but it was 2. Is that correct?

Upvotes: 0

Views: 474

Answers (1)

David Heffernan
David Heffernan

Reputation: 613572

This is not your real code. For a start it does not compile. You have omitted the quote marks. If we fix that then we have:

len := TRegEx.Create('[_a-zA-Z][_a-zA-Z0-9]*').Match('3a').Length;

But that returns a value of 1 and not 2 as you stated. This return value is correct because the a matches [_a-zA-Z] and then the input string ends.

I expect that you have the wrong regex. Perhaps you should be using

^[_a-zA-Z][_a-zA-Z0-9]*$

The ^ matches the beginning of the input string, the $ mathes the end. Presumably the input is taken from a source code tokenizer.

So the conclusion is that there is no bug evident in the Delphi regex code from this pattern and input.

Upvotes: 1

Related Questions