return
return

Reputation: 1049

Help in set RegularExpressions in Delphi XE

i want to set RegularExpressions for check string1.

string1 can change to :

in string1 only Character 'D' and semicolon is always exist.

i set RegularExpressions1 := '\b(D#\;#)\b';

but RegularExpressions1 can't to check string1 correctly.

in the vb6 this RegularExpressions1="D#;#". but i don't know that is in Delphi??

Upvotes: 0

Views: 753

Answers (2)

Jan Goyvaerts
Jan Goyvaerts

Reputation: 21999

Assuming both numbers require at least one digit, use this regex:

\AD\d+;\d+\z

I prefer to use \A and \z instead of ^ and $ to match the start and end of the string because they always do only that.

In Delphi XE you can check whether this regex matches string1 in a single line of code:

if TRegEx.IsMatch(string1, '\AD\d+;\d+\z') then ...

If you want to use many strings, intantiate the TRegEx:

var RE: TRegEx;

RegEx.Create('\AD\d+;\d+\z'); for string1 in ListOfStrings do if RE.IsMatch(string1) then ...

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336178

Try

\bD\d*;\d*

\d* means "zero or more digits".

By the way, I have omitted the second \b because otherwise the match would fail if there is no number after the semicolon (and you said the number was optional).

If by "check" you mean "validate" an entire string, then use

^D\d*;\d*$

All this assumes that only digits are allowed after D and ;. If that is not the case, please edit your question to clarify.

Upvotes: 3

Related Questions