473,395 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 37124
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ST Wong | last post by:
Hi all, As metacharaters ^ and $ are not used in regular expression in XML, I'm looking for how to specify empty string. Would anyone please help? Thanks a lot. Regards, ST Wong
1
by: Earl Teigrob | last post by:
I am parsing some HTML and need to match certain tag attributes to change from relative to absolute urls. The search string I am currently using is rex="src\\s*=\\s*\"(?<content>.+?)\""; that...
7
by: Allerdyce.John | last post by:
Hi, I am new to python. I would like to know how to use python regular expression to substitute string value? I have an input string like this: x:11 y:0 w:760 h:19 area:14440 areaPerCent:0...
7
by: Kevin CH | last post by:
Hi, I'm currently running into a confusion on regex and hopefully you guys can clear it up for me. Suppose I have a regular expression (0|(1(01*0)*1))* and two test strings: 110_1011101_ and...
15
by: Mark Rae | last post by:
Hi, I'm trying to construct a RegEx pattern which will validate a string so that it can contain: only the numerical characters from 0 to 9 i.e. no decimal points, negative signs, exponentials...
20
by: Serge Rielau | last post by:
I'm playing with the ICU library (slow weekend :-) and it's quite simple to expose the regular expression functionality for Unicode databases. Presuming that the majority is not on unicode, would...
6
by: rahatekarabhijeet | last post by:
I want regular expression which can extract the data between two symbols, e.g: <*skdjflsk (new line character) lksdjfklasjdfl (new line character) ksjdfkhasdkf (new line character) asdfjkhasdjk...
2
by: yoni | last post by:
Hey I need the regexp pattern to match a single quoted string. its a bit tricky since double single quotes in the middle are part of the string, and we should continue to capture till the next...
7
by: blaine | last post by:
Hey everyone, For the regular expression gurus... I'm trying to write a string matching algorithm for genomic sequences. I'm pulling out Genes from a large genomic pattern, with certain start...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.