Solved

Regular expansion

Posted on 2001-09-08
22
141 Views
Last Modified: 2006-11-17
I need at regular expansion strnig that will only accept

number from 0 to 100

and it may include ,

examples:

12
44
65
34
32
54
42,3
54,
99,9
23,1
1,3
0,3
100
0
Comment
Question by:5
  • 10
  • 5
  • 3
  • +2
22 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 6467180
"regular expansion"?  What do you mean?  Why cant you simply do:

If Number < 0 Or Number > 100 Then
     Reject
Else
     Accept
End If
0
 
LVL 12

Expert Comment

by:roverm
ID: 6467206
If you are trying to check whether a string contains a number, use this:

If IsNumeric(sString) Then
  Msgbox "It's numeric"
Else
  Msgbox "It NOT numeric"
End If

D'Mzzl!
RoverM
0
 
LVL 12

Expert Comment

by:roverm
ID: 6467209
And, of course then check, like AzraSound, for the number to be within your range.

D'Mzzl!
RoveRM
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:5
ID: 6467220
no...its gotta be regular expansion!
0
 
LVL 12

Expert Comment

by:roverm
ID: 6467227
Please explain what you mean by 'regular expansion'.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6467229
I didn't use it for real, so here is a trial.  :-)
/d is the same as [0-9] - digit
/b is boundary
| is or operator

Btw, it doesn't work for 154,11 - should ignore it, but it finds 11 as match.

' add reference to "Microsoft VBSript Regular Expressions"
' add multiline textbox
Option Explicit

Private Sub Form_Click()
    Dim re As New RegExp
    Dim ma As Match
    Cls
   
    re.Pattern = "\b(\d\d,\d\d|\d\d,\d|\d\d|\d,\d\d|\d,\d|\b\d)\b"
    re.Global = True             ' find all the occurrences
    For Each ma In re.Execute(Text1.Text)
        Print "Found '" & ma.Value & "' at index " & ma.FirstIndex
    Next

End Sub

Private Sub Form_Load()
    Text1.Move 3000, 0, ScaleWidth - 3000, ScaleHeight
    Text1.Text = "12 444 655 3" & vbCrLf & "32,2 154,11 42,3  54," & vbCrLf & "100 9,9 8823,1 0,3"
End Sub
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6467243
Oh you meant regular expression?  Why do you have to use REs?
0
 

Author Comment

by:5
ID: 6467278
yes Regular Expressions was what i meant..im sorry!

ameba>>>Nice, but its only one number that needs validation, so that wont work...can you rewrite it?
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6467286
Again, why do you need to use REs?  Youre adding another component to the package that may not even be required.  REs are generally used for parsing text to find particular patterns, but not for testing whether a value lies between a certain bounds.  What is the form of your input?
0
 
LVL 15

Expert Comment

by:ameba
ID: 6467321
No I cannot  :-) still the same problem with  comma being the boundary.

' add reference to "Microsoft VBSript Regular Expressions"
' add textbox
Option Explicit

Private Sub Form_Click()
    Dim re As New RegExp
    Dim ma As Match
    Cls
    re.Pattern = "\b(\d{1,2},\d{1,2})\b" ' & "|" & "\b\d{1,2}\b"
    Print re.Test(Text1.Text)
End Sub

Private Sub Form_Load()
    Text1.Move 3000, 0, ScaleWidth - 3000, ScaleHeight
    Text1.Text = "99,9"
End Sub
0
 
LVL 15

Accepted Solution

by:
ameba earned 300 total points
ID: 6467462
Without boundaries \b
^ marks beginning, $ end of test string

' add textbox
Option Explicit

Private Sub Form_Load()
    Text1.Text = "99,9"
End Sub

Private Sub Text1_Change()
    Dim re As New RegExp
    Dim ma As Match
    Dim ptn1 As String, ptn2 As String
   
    ptn1 = "\d{1,2}\,\d{0,2}"    ' #,  to ##,##
    ptn2 = "\d{1,2}"            ' #  or  ##
   
    re.Pattern = "^(" & ptn1 & "|" & ptn2 & ")$"
    Caption = re.Test(Text1.Text)
End Sub
0
 
LVL 15

Expert Comment

by:ameba
ID: 6467467
> ^ marks beginning
or negation, depends on its position
0
 

Author Comment

by:5
ID: 6468527
needed a lil extra..but now its perfect.-..thank you very much!
^(\d{1,2}\,\d{0,2}|\d{1,2})$|100
0
 
LVL 15

Expert Comment

by:ameba
ID: 6468550
> |100
Than 5100 (or 1001) will pass the test.  Make it:
|^100$

Thanks for the points.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6468781
I am as curious as AzraSound.  Why do you need a complex regular expression to test an expression that would take very simple code to test?  Remember there are a lot of benefits to keeping it simple.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6469126
Universal validation?

It is easy to use regexp to check SSN, Zip-code, Date, FileName, Email, Range...
So, instead of complex rules and extra properties (Min, Max, Required) for each field, use only one property: ValidationExpression
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6469185
You still need to define the search pattern based on what is being validated though, correct?  Is it much different than doing the same w/o REs?  Dont get me wrong, I know REs can be powerful in certain situations but I failed to see its benefit in this case, (i.e. only accepting a number between 0 and 100)
0
 
LVL 15

Expert Comment

by:ameba
ID: 6469220
I am not sure I understand you.  Of course you must define pattern, but it will be simplification in other steps/objects.
To save all fields:
   For Each x in Fields
        If Test(x, x.pattern)= False Then GoTo BadInput
   Next

and Test() is simple - check only one thing using RegExp

Without that you will have to use more complex system:

If x.type = numeric
   test for min, max
elseIf x.type = date
   test using IsDate, and test date range
elseIf x.type = ...
   test
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6469355
I guess I just find it just as easy to write a function and do:

For Each x In Fields
   If Test(x, x.type) = False Then GoTo BadInput
Next


where Test can handle conditional logic based on the type of validation to perform.  I see where you are coming from, and have nothing against your solution.  I only questioned the overkill in using REs for this particular question (to which we were never fully told how/why/what the actual source of data/problem was).
0
 
LVL 15

Expert Comment

by:ameba
ID: 6469469
Maybe we'll hear the real reason for using it...
I hope it is not like FSO which is over-used for simple tasks.

Ouch, I just noticed that RegExp is part of biiig VBSCRIPT.DLL ...
0
 
LVL 15

Expert Comment

by:ameba
ID: 6501294
or maybe not.  I was hoping this thing runs two-way.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6514925
Last trial... .  :-)
The reason for using RE is ... ?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question