martes
martes

Reputation: 86

Excel VBA Regex Check For Repeated Strings

I have some user input that I want to validate for correctness. The user should input 1 or more sets of characters, separated by commas.

So these are valid input

these are invalid

I googled for a regex pattern to this and found a possible pattern that tested for a recurring instance of any 3 characters, and I modified like so

/^(.{4,}).*\1$/

but this is not finding matches.

I can manage the last comma that may or may not be there before passing to the test so that it is always there.

Preferably, I would like to test for letters (any case) and numbers only, but I can live with any characters.

I know I could easily do this in straight VBA splitting the input on a comma delimiter and looping through each character of each array element, but regex seems more efficient, and I will have more cases than have slightly different patterns, so parameterising the regex for that would be better design.

TIA

Upvotes: 4

Views: 1060

Answers (2)

Amorpheuses
Amorpheuses

Reputation: 1423

The regular expression

"^[\w]{4}(,[\w]{4})*$" 

should work.

You can try this to see whether it works for all your cases using the following function. Assuming your test strings are in cells A1 thru A5 on the spreadsheet:

Sub findPattern()
  Dim regEx As New RegExp
  regEx.Global = True
  regEx.IgnoreCase = True
  regEx.Pattern = "^[\w]{4}(,[\w]{4})*$"
  Dim i As Integer
  Dim val As String
  For i = 1 To 5:
    val = Trim(Cells(i, 1).Value)
    Set mat = regEx.Execute(val)
    If mat.Count = 0 Then
      MsgBox ("No match found for " & val)
    Else
      MsgBox ("Match found for " & val)
    End If
 Next
End Sub

Upvotes: 2

Doug Glancy
Doug Glancy

Reputation: 27478

I believe this does what you want:

^([A-Z|a-z|0-9]{4},)*[A-Z|a-z|0-9]{4}$

It's a line beginning followed by zero or more groups of four letters or numbers ending with a comma, followed by one group of four letters or number followed by an end-of-line.

You can play around with it here: https://regex101.com/r/Hdv65h/1

Upvotes: 3

Related Questions