简体   繁体   中英

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

  • COM1
  • COM1,COM2,1234

these are invalid

  • COM -- only 3 characters
  • COM1,123 -- one set is only 3 characters
  • COM1.1234,abcd -- a dot separator not comma

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM