views:

98

answers:

2

hi,

i want to set RegularExpressions for check string1.

string1 can change to :

  • string1:='D1413578;1038'
  • string1:='D2;11'
  • string1:='D16;01'
  • ,....

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??

+3  A: 

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.

Tim Pietzcker
+1 for BEST avitar icon EVER. Very cool idea.
Chris Thornton
@Chris, Tim: What information does it contain? Is it ASCII?
Andreas Rejbrand
@Andreas - it's the URL to his website, the same one listed in his profile. The mini one shown here won't scan on my droid, but the larger one on his profile pick scans and my droid offers to take me to his site. Very cool.
Chris Thornton
A: 

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 ...

Jan Goyvaerts