Solved

Regular expansion

Posted on 2001-09-08
22
140 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

839 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