A4eIT
asked on
Regular Expression - Help!!
hi
i need to write a regular expression that will do the following:
allow input of any combination of ABCD without duplicates,
ie A, B, C, D, AB, AC, AD, ABC, ABD, ABCD, DBA, DAB, DA etc, but NOT AA, BAB, ABCC, (any duplicated letters)
please help, 500 points!
many thanks
i need to write a regular expression that will do the following:
allow input of any combination of ABCD without duplicates,
ie A, B, C, D, AB, AC, AD, ABC, ABD, ABCD, DBA, DAB, DA etc, but NOT AA, BAB, ABCC, (any duplicated letters)
please help, 500 points!
many thanks
ASKER
hiya
thanks for that
it can be ANY 4 letters.
is this still the best way to handle this?
cheers
thanks for that
it can be ANY 4 letters.
is this still the best way to handle this?
cheers
Yea, that'll check any letter for duplicates. (It still might not be the easiest way to do it, but it works)...
I just noticed a few errors in my code though... Use this instead:
'------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -----
Function Verify(ByVal strExpression As String) As Boolean
Dim i As Integer
Dim LoopTwo As Integer
Dim CharArr As New ArrayList
For i = 1 To Len(strExpression)
For LoopTwo = 0 To (CharArr.Count - 1)
If Strings.Mid(strExpression, i, 1) = CharArr(LoopTwo) Then
Return False
Exit Function
End If
Next LoopTwo
CharArr.Add(Strings.Mid(st rExpressio n, i, 1))
Next i
Return True
End Function
'------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -----
I've changed it to a function to return True or False. True if it passes validation, and False if contains ANY duplicate letters. You can call this function from any other procedure.
Theres nothing to check how many letters the string holds, if you want to add this, set the "MaxLength" property of the text box to '4', alternatively just put the following line before the first 'For' statement:
If strExpression.Length > 4 then
Return False
Exit Function
End If
Regards,
Dez
I just noticed a few errors in my code though... Use this instead:
'-------------------------
Function Verify(ByVal strExpression As String) As Boolean
Dim i As Integer
Dim LoopTwo As Integer
Dim CharArr As New ArrayList
For i = 1 To Len(strExpression)
For LoopTwo = 0 To (CharArr.Count - 1)
If Strings.Mid(strExpression,
Return False
Exit Function
End If
Next LoopTwo
CharArr.Add(Strings.Mid(st
Next i
Return True
End Function
'-------------------------
I've changed it to a function to return True or False. True if it passes validation, and False if contains ANY duplicate letters. You can call this function from any other procedure.
Theres nothing to check how many letters the string holds, if you want to add this, set the "MaxLength" property of the text box to '4', alternatively just put the following line before the first 'For' statement:
If strExpression.Length > 4 then
Return False
Exit Function
End If
Regards,
Dez
Try this
Private Function OnlyOnce(ByVal fullString As String, ByVal findString As String) As Boolean
Dim First As Integer = fullString.IndexOf(findStr ing)
Return First > -1 AndAlso First = fullString.LastIndexOf(fin dString)
End Function
It looks for the first occurrence of the 'findString' in the "fullString", and then returns True provided both that there is such an occurrence and that there is no later occurrence. Otherwise it returns False.
Roger
Private Function OnlyOnce(ByVal fullString As String, ByVal findString As String) As Boolean
Dim First As Integer = fullString.IndexOf(findStr
Return First > -1 AndAlso First = fullString.LastIndexOf(fin
End Function
It looks for the first occurrence of the 'findString' in the "fullString", and then returns True provided both that there is such an occurrence and that there is no later occurrence. Otherwise it returns False.
Roger
Ignore that. I misread the question. Even so, you might find an improvement on Dez's code with use of IndexOf rather than looping
Roger
Roger
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hiya
many thanks for your quick responses to this, much appreciated.
i adopted ZeonFlash's approach in the end, and caught in in the KeyPress event so that the textbox would never accept any invalid characters or duplicates.(see below fyi)
many thanks again, also to DjDezmond
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPr essEventAr gs) Handles TextBox1.KeyPress
Dim fullString As String = TextBox1.Text & CStr(e.KeyChar).ToUpper
If Not System.Text.RegularExpress ions.Regex .IsMatch(f ullString, "^[A-D]{1,4}$") Then
e.Handled = True
Exit Sub
Else
For i As Integer = 0 To (fullString).Length - 1
If fullString.IndexOf(fullStr ing.Chars( i)) <> fullString.LastIndexOf(ful lString.Ch ars(i)) Then
e.Handled = True
Exit Sub
End If
Next
End If
e.Handled = False
End Sub 'TextBox1_KeyPress
many thanks for your quick responses to this, much appreciated.
i adopted ZeonFlash's approach in the end, and caught in in the KeyPress event so that the textbox would never accept any invalid characters or duplicates.(see below fyi)
many thanks again, also to DjDezmond
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPr
Dim fullString As String = TextBox1.Text & CStr(e.KeyChar).ToUpper
If Not System.Text.RegularExpress
e.Handled = True
Exit Sub
Else
For i As Integer = 0 To (fullString).Length - 1
If fullString.IndexOf(fullStr
e.Handled = True
Exit Sub
End If
Next
End If
e.Handled = False
End Sub 'TextBox1_KeyPress
There may be a easier way to do this, but heres my pennys worth...
'-------------------------
Sub Verify(Byval strExpression as string)
Dim i as integer
Dim CharArr as New ArrayList
For i = 1 to len(strExpression)
CharArr.Add Strings.Mid(strExpression,
For LoopTwo = 0 to (UBound(CharArr) - 1)
If Strings.Mid(strExpression,
Goto DuplicateHndlr
End If
Next LoopTwo
Next i
Exit Sub
DuplicateHndlr:
'//Add Code here to handle duplicate entry...
MsgBox "Duplicate Entry found!"
End Sub
'-------------------------
Hope this helps,
Dez