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
Visual Basic.NET

Avatar of undefined
Last Comment
A4eIT

8/22/2022 - Mon
DjDezmond

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
A4eIT

ASKER
hiya

thanks for that

it can be ANY 4 letters.

is this still the best way to handle this?

cheers
DjDezmond

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Sancler

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
Sancler

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
ZeonFlash

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.