Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression question 1

Status
Not open for further replies.

fgeorge

Programmer
Jun 28, 2002
76
NG
i need a regular expression that will validate the following:
1)must contain 15 digits..
2)all numbers must begin with 234803
3)
must not be;
234803200
234803201
234803202
234803209

thx
 
Try something like this:
Code:
varOK=False
If  Len(var)=15 And IsNumeric(var) _
And Left(var,6)="234803" Then
  tst=Mid(var,7,3)
  If  tst<>&quot;200&quot; And tst<>&quot;201&quot;  _
  And tst<>&quot;202&quot; And tst<>&quot;209&quot;  Then
    varOK=True
  End If
End If
You can encapsulate this snippet with a boolean function.

Hope This Help
PH.
 
so in actuality you want a string (but numeric) validated as
&quot;234803&quot; plus a max/min of 9 more numerics? ignoring the nine digit examples posted!

first step is to write the function obviously


Function RegExp(str)
Dim regEx, retVal
Set regEx = New RegExp
regEx.Pattern =
regEx.IgnoreCase = False
retVal = regEx.Test(str)
If retVal Then
RegExp = &quot;The value tested ok&quot;
Else
RegExp = &quot;The value was not valid&quot;
End If
End Function

alert(RegExp(&quot;234803123456789&quot;))

this is the most common structure to a test method in vbscript on regular expressions.
the pattern is failry simple
the unchanged string
&quot;(234803)&quot;
Then you need 9 more numeriuc values and only 9
\d = any numeric value (or you can do [0-9]
then the { } for the count
{9}
don't forget the + the ^ marking the start of the string and the $ to mark the end
and there you have it
&quot;^(234803)+[\d]{9}$&quot;

_____________________________________________________________________
You can accomplish anything in life, provided that you do not mind who gets credit.
Harry S. Truman

onpnt2.gif

 
erm. forgot to jsut add the InStr
you could nest them but to keep it clean
InStr(str,&quot;234803201&quot;)
eg
Function RegExp(str)
Dim regEx, retVal
If Instr(str,&quot;234803200&quot;) or InStr(str,&quot;234803201&quot;) > 0 Then
alert(&quot;error&quot;)

Else

Set regEx = New RegExp
regEx.Pattern = &quot;^(234803)+[\d]{9}$&quot;
regEx.IgnoreCase = False
retVal = regEx.Test(str)
If retVal Then
RegExp = &quot;The value tested ok&quot;
Else
RegExp = &quot;The value was not valid&quot;
End If
End If
End Function

alert(RegExp(&quot;s234803201456789&quot;))

_____________________________________________________________________
You can accomplish anything in life, provided that you do not mind who gets credit.
Harry S. Truman

onpnt2.gif

 
the next 3 digits after 234803 must not be 200,201,203 or 209..
 
hello onpnt!!

if i use ^(234803)+[\d]{9}$ the number &quot;54&quot; is allowed through..
this should not be so...
the number 2348032003456 is allowed through too...
any one help?
 
the number 54 was not allowed here. have you cahnged the function at all? can you post the function?

It is impossible for 54 really to go through. the ^ indicates that being a false test as ^23 must be the starting numeric value

also, this line
If Instr(str,&quot;234803200&quot;) or InStr(str,&quot;234803201&quot;) > 0 Then
is where you're 200,201 ect.. is not allowed

unless you want to do multi tests that would be how I would procede

_____________________________________________________________________
You can accomplish anything in life, provided that you do not mind who gets credit.
Harry S. Truman

onpnt2.gif

 
Use this as the pattern in onpnt's code, and then you can drop the instr component completely:

&quot;^23480320[3-8][\d]{6}$&quot;
 
the simplification of that is genius in itself. we so often think to large and forget the small bytes that we can use in our favor.

* for the tip on that strongm

_____________________________________________________________________
You can accomplish anything in life, provided that you do not mind who gets credit.
Harry S. Truman

onpnt2.gif

 
I don't know what is the opinion of fgeorge but the
Code:
 &quot;^23480320[3-8][\d]{6}$&quot;
don't allow all the permissible values. The 20 in position 7-8 is not mandatory IMHO.
 
ha, but the building foundation is there. my greatest fear to bring professionals here at TT is a copde post and nothing learning from your visits and problem solving instances with the ablility to work with other professionals as ourselves.

in strongm bringing to the white board the fact not to validate the long value of a 200, 201 scenario and taking it down a step to saying, &quot;why not simply the digit?&quot;

say +[\d]{3}| not (20)[\d] Then giving the factor that the long way is not always the better but the one factor that may be unique and singled out can be the pointing tool.

well, that enough chibberish [wink]

_____________________________________________________________________
You can accomplish anything in life, provided that you do not mind who gets credit.
Harry S. Truman

onpnt2.gif

 
jsut to further everything also. with strongm's suggestion and then PHV's points. Further back to my stating the multi tests you could very easy switch the logic in the RegEx function with two test's while changing the objects pattern.
basically from strongm you can use
regEx.Pattern = &quot;^(234803)+(20)+[\d]{1,}$&quot;
to .test and if true is returned then you have the values not wnated. although if that was not true and returned false you can use the other pattern by setting the pattern to the
regEx.Pattern = &quot;^(234803)+[\d]{9}$&quot;
along the same of testing true to false giving the appropriate response from the function.

_____________________________________________________________________
You can accomplish anything in life, provided that you do not mind who gets credit.
Harry S. Truman

onpnt2.gif

 
Whoops...missed that requirement when reading the question. Try:

&quot;^234803(20[3-8]|[^2][^0]\d)\d{6}$&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top