Regular Expression

  • Thread starter Thread starter brian
  • Start date Start date
B

brian

I am trying to write a reqular expression to validate the
following:

Needs to be 6 numbers
First number between 1-4
Second number between 1-8
4-6 Numbers any number

^[1-4]|[1-8]/d{1}/d{1}/d{1}/d{1}$

I tested the following on http://www.regexlib.com and the
expression works fine. But I test in in Visual Studio
and I keep getting the error message I assigned to the
conrol.

Does anyone have any ideas?
 
Hi Brian,

Try it this way?

^[1-4]{1}[1-8]{1}\d{1}\d{1}\d{1}\d{1}$

^ (anchor to start of string)
Any character in "1-4"
Exactly 1 times
Any character in "1-8"
Exactly 1 times
Any digit
Exactly 1 times
Any digit
Exactly 1 times
Any digit
Exactly 1 times
Any digit
Exactly 1 times
$ (anchor to end of string)


I use Eric Gunnerson's excellent Regular Expression Workbench which is free
here:

http://www.gotdotnet.com/Community/...mpleGuid=C712F2DF-B026-4D58-8961-4EE2729D7322
 
OR, you could consolidate and simplify:
^[1-4][1-9]\d{2}

:)


Ken Cox said:
Hi Brian,

Try it this way?

^[1-4]{1}[1-8]{1}\d{1}\d{1}\d{1}\d{1}$

^ (anchor to start of string)
Any character in "1-4"
Exactly 1 times
Any character in "1-8"
Exactly 1 times
Any digit
Exactly 1 times
Any digit
Exactly 1 times
Any digit
Exactly 1 times
Any digit
Exactly 1 times
$ (anchor to end of string)


I use Eric Gunnerson's excellent Regular Expression Workbench which is free
http://www.gotdotnet.com/Community/...mpleGuid=C712F2DF-B026-4D58-8961-4EE2729D7322




brian said:
I am trying to write a reqular expression to validate the
following:

Needs to be 6 numbers
First number between 1-4
Second number between 1-8
4-6 Numbers any number

^[1-4]|[1-8]/d{1}/d{1}/d{1}/d{1}$

I tested the following on http://www.regexlib.com and the
expression works fine. But I test in in Visual Studio
and I keep getting the error message I assigned to the
conrol.

Does anyone have any ideas?
 
First, there is an error in your logic--you need to remove the "|" between
[1-4] and [1-8].

2nd, the expression can be simplifed as ^[1-4][1-8]\d{4}

Third, if you're using this expression Javascript, you may need to escape
the expression.

Example:

var regExStr = "^[1-4][1-8]\d{4}"; // should be escaped:
^[1-4][1-8]\\d{4}
alert(regExStr); // shows ^[1-4][1-8]d{4}


Original matches 1 OR 88901 but not 98901
^ // Beginning of string
[1-4] // must be 1-4
| OR
[1-8] // [1-8
/d{1}/d{1}/d{1}/d{1} //4 digits \d{4}
$ // end of string

Chris
 
Back
Top