Regular expansion

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
5Asked:
Who is Participating?
 
amebaConnect With a Mentor Commented:
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
 
AzraSoundCommented:
"regular expansion"?  What do you mean?  Why cant you simply do:

If Number < 0 Or Number > 100 Then
     Reject
Else
     Accept
End If
0
 
rovermCommented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
rovermCommented:
And, of course then check, like AzraSound, for the number to be within your range.

D'Mzzl!
RoveRM
0
 
5Author Commented:
no...its gotta be regular expansion!
0
 
rovermCommented:
Please explain what you mean by 'regular expansion'.
0
 
amebaCommented:
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
 
AzraSoundCommented:
Oh you meant regular expression?  Why do you have to use REs?
0
 
5Author Commented:
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
 
AzraSoundCommented:
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
 
amebaCommented:
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
 
amebaCommented:
> ^ marks beginning
or negation, depends on its position
0
 
5Author Commented:
needed a lil extra..but now its perfect.-..thank you very much!
^(\d{1,2}\,\d{0,2}|\d{1,2})$|100
0
 
amebaCommented:
> |100
Than 5100 (or 1001) will pass the test.  Make it:
|^100$

Thanks for the points.
0
 
PaulHewsCommented:
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
 
amebaCommented:
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
 
AzraSoundCommented:
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
 
amebaCommented:
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
 
AzraSoundCommented:
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
 
amebaCommented:
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
 
amebaCommented:
or maybe not.  I was hoping this thing runs two-way.
0
 
amebaCommented:
Last trial... .  :-)
The reason for using RE is ... ?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.