472,121 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,121 software developers and data experts.

Regular Expression: matching an empty string

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.

Feb 6 '07 #1
6 36938
BillAtWork wrote:
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)
{
...
}
Feb 6 '07 #2
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" wrote:
BillAtWork wrote:
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)
{
...
}
Feb 6 '07 #3
Hi,

try this:

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

hth
Christof

"BillAtWork" <Bi********@nospam.nospamschrieb im Newsbeitrag
news:79**********************************@microsof t.com...
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.

Feb 6 '07 #4
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" wrote:
Hi,

try this:

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

hth
Christof

"BillAtWork" <Bi********@nospam.nospamschrieb im Newsbeitrag
news:79**********************************@microsof t.com...
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.


Feb 6 '07 #5
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
"BillAtWork" <Bi********@nospam.nospamschrieb im Newsbeitrag
news:79**********************************@microsof t.com...
>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.

Feb 6 '07 #6
Changed it :) Thank you!

"Hans Kesting" wrote:
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
"BillAtWork" <Bi********@nospam.nospamschrieb im Newsbeitrag
news:79**********************************@microsof t.com...
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.


Feb 6 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by ST Wong | last post: by
1 post views Thread by Earl Teigrob | last post: by
7 posts views Thread by Kevin CH | last post: by
20 posts views Thread by Serge Rielau | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.