All I am trying to do is use Regex.IsMatch to validate that a string is
either empty, or exactly 4 characters (digits) in length. In other words,
the strings, "", and "1234" should pass, whereas "123", "abcd", and "12345"
should fail. I know there has to be a simple answer to this, but right now
I can't find it. I tried using:
if (str.Length == 0)
return true;
if (Regex.IsMatch(str, "[0-9]^4"))
return true;
but this does not work. Variations such as
Regex.IsMatch("1234", @"\d\d\d\d") come close, but also result in "12345"
validating.
Any help would be greatly appreciated. 2 28649
Hi Byron,
You have to use ^ and $ to indicate start and end of the string:
Regex.IsMatch("1234", @"^\d\d\d\d$")
should do the trick.
Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Byron" <si*****@ctcorp.com> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl... All I am trying to do is use Regex.IsMatch to validate that a string is either empty, or exactly 4 characters (digits) in length. In other words, the strings, "", and "1234" should pass, whereas "123", "abcd", and
"12345" should fail. I know there has to be a simple answer to this, but right
now I can't find it. I tried using:
if (str.Length == 0)
return true;
if (Regex.IsMatch(str, "[0-9]^4"))
return true;
but this does not work. Variations such as
Regex.IsMatch("1234", @"\d\d\d\d") come close, but also result in "12345" validating.
Any help would be greatly appreciated.
That did the trick. Thanks for the quick reply
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:OF*************@TK2MSFTNGP12.phx.gbl... Hi Byron,
You have to use ^ and $ to indicate start and end of the string: Regex.IsMatch("1234", @"^\d\d\d\d$")
should do the trick.
Cheers,
-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation
"Byron" <si*****@ctcorp.com> wrote in message news:e0**************@TK2MSFTNGP10.phx.gbl... All I am trying to do is use Regex.IsMatch to validate that a string is either empty, or exactly 4 characters (digits) in length. In other
words, the strings, "", and "1234" should pass, whereas "123", "abcd", and "12345" should fail. I know there has to be a simple answer to this, but right now I can't find it. I tried using:
if (str.Length == 0)
return true;
if (Regex.IsMatch(str, "[0-9]^4"))
return true;
but this does not work. Variations such as
Regex.IsMatch("1234", @"\d\d\d\d") come close, but also result in
"12345" validating.
Any help would be greatly appreciated.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Henry |
last post by:
I have this simple code,
string escaped = Regex.Escape( @"`~!@#$%^&*()_=+{}\|;:',<.>/?" + "\"" );
string input = @"a&+" + "\"" + @"@(-d)\e";
Regex re = new Regex( string.Format(@"(+)", escaped),...
|
by: Colin Reid |
last post by:
Hey MS, here's an apparent problem in the base class library. I pulled
the email validation pattern
"^((*)*@(()+(*)*\.)+{2,9})"
from http://regexlib.com.
If I validate the email address...
|
by: Doug |
last post by:
I'm a little confused by this functionality. It doesn't seem to be
behaving like it should.
I am using the following regular expression to validate email
addresses:...
|
by: live your lives |
last post by:
i am trying to validate a simple username textbox using RegularExpressionValidator:
TextBox tbUserName = new TextBox();
tbUserName.ID = "tbUserName";
string strPatternUserName = @"\W";
//...
|
by: Jon Maz |
last post by:
Hi All,
Am getting frustrated trying to port the following (pretty simple) function
to CSharp. The problem is that I'm lousy at Regular Expressions....
//from...
|
by: Dennis |
last post by:
Should not the following return False if s="255xxxxyyy"? It seems to return
True.
Regex.IsMatch(s, "")
--
Dennis in Houston
|
by: ad |
last post by:
I am useing VS2005 to develop wep application.
I use a RegularExpress both in RegularExpressionValidator and Regex class to
validate a value.
The RegularExpress is 20|\-9|\-1|?\d{1}
When I...
|
by: Tidane |
last post by:
Visual Basic.NET Framework 2.0
I've created a program to parse out text as the program recieved it and use Regex matching to decide what should be done. My problem is that the text is matching when...
|
by: =?Utf-8?B?amFj?= |
last post by:
Hi,
I have problems with following code and don’t find the bug :
// Set
ArrayList aArray = new ArrayList();
regStr = new Regex(@"\?)*(\d+)\]");
if(text != null && regStr.IsMatch(text))...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |