Regular Expression: matching an empty string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
Thought this would be simple! If I want to validate a field that can contain
"z" followed by 3 digits OR it's a completely empty string, how do I do that?
The z123 bit is simple, but the empty string is giving me problems! e.g.

^[zZ]\d{3}$|

....doesn't work (I thought the trailing "|" would detect the empty string).

Thanks.
 
BillAtWork said:
Hi,
Thought this would be simple! If I want to validate a field that can
contain "z" followed by 3 digits OR it's a completely empty string,
how do I do that? The z123 bit is simple, but the empty string is
giving me problems! e.g.

^[zZ]\d{3}$|

...doesn't work (I thought the trailing "|" would detect the empty
string).

if( 0 == theString.length || <pattern match> )
{
...
}
 
Hi - unfortunately I cannot use that method...if this had been entirely in C#
then yes that's the way to go. However, this validation is driven entirely by
an XML validation file (called from a C# app). I need to use a single regular
expression that will perform that vaildation.

Ebbe Kristensen said:
BillAtWork said:
Hi,
Thought this would be simple! If I want to validate a field that can
contain "z" followed by 3 digits OR it's a completely empty string,
how do I do that? The z123 bit is simple, but the empty string is
giving me problems! e.g.

^[zZ]\d{3}$|

...doesn't work (I thought the trailing "|" would detect the empty
string).

if( 0 == theString.length || <pattern match> )
{
...
}
 
Perfect! Thank you!

This is the second regexp question I've had over the past few days and the
answers I've received have shown me I have to stop thinking so procedurally
and get a bit more "declarative" and "set" oriented. Such a simple solution
created just by looking at the problem a little differently, as you have.

Christof Nordiek said:
Hi,

try this:

^([zZ]\d{3})*$

hth
Christof

BillAtWork said:
Hi,
Thought this would be simple! If I want to validate a field that can
contain
"z" followed by 3 digits OR it's a completely empty string, how do I do
that?
The z123 bit is simple, but the empty string is giving me problems! e.g.

^[zZ]\d{3}$|

...doesn't work (I thought the trailing "|" would detect the empty
string).

Thanks.
 
Hi,
try this:

^([zZ]\d{3})*$

wouldn't ^([zZ]\d{3})?)$ be better?
the "*" would cause it to match 0 or *more* times, so Z123z456 would also
match.
the "?" would match 0 or 1 times.

Hans Kesting
hth
Christof
Hi,
Thought this would be simple! If I want to validate a field that can
contain
"z" followed by 3 digits OR it's a completely empty string, how do I
do
that?
The z123 bit is simple, but the empty string is giving me problems!
e.g.
^[zZ]\d{3}$|

...doesn't work (I thought the trailing "|" would detect the empty
string).

Thanks.
 
Changed it :) Thank you!

Hans Kesting said:
Hi,

try this:

^([zZ]\d{3})*$

wouldn't ^([zZ]\d{3})?)$ be better?
the "*" would cause it to match 0 or *more* times, so Z123z456 would also
match.
the "?" would match 0 or 1 times.

Hans Kesting
hth
Christof
Hi,
Thought this would be simple! If I want to validate a field that can
contain
"z" followed by 3 digits OR it's a completely empty string, how do I
do
that?
The z123 bit is simple, but the empty string is giving me problems!
e.g.
^[zZ]\d{3}$|

...doesn't work (I thought the trailing "|" would detect the empty
string).

Thanks.
 
Back
Top