Link to home
Start Free TrialLog in
Avatar of A4eIT
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
Avatar of DjDezmond
DjDezmond
Flag of United Kingdom of Great Britain and Northern Ireland image

Will the expression only ever be ABCD etc, or can it be any 4 letters (without duplicates of course)?

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, i, 1)    

    For LoopTwo = 0 to (UBound(CharArr) - 1)
       If Strings.Mid(strExpression, i, 1) = CharArr(i).ToString() then
         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
Avatar of A4eIT
A4eIT

ASKER

hiya

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(strExpression, 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
Try this

    Private Function OnlyOnce(ByVal fullString As String, ByVal findString As String) As Boolean
        Dim First As Integer = fullString.IndexOf(findString)
        Return First > -1 AndAlso First = fullString.LastIndexOf(findString)
    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
ASKER CERTIFIED SOLUTION
Avatar of ZeonFlash
ZeonFlash

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of A4eIT

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.KeyPressEventArgs) Handles TextBox1.KeyPress

        Dim fullString As String = TextBox1.Text & CStr(e.KeyChar).ToUpper

        If Not System.Text.RegularExpressions.Regex.IsMatch(fullString, "^[A-D]{1,4}$") Then
            e.Handled = True
            Exit Sub
        Else
            For i As Integer = 0 To (fullString).Length - 1
                If fullString.IndexOf(fullString.Chars(i)) <> fullString.LastIndexOf(fullString.Chars(i)) Then
                    e.Handled = True
                    Exit Sub
                End If
            Next
        End If

        e.Handled = False

    End Sub 'TextBox1_KeyPress