简体   繁体   中英

Trouble with what precedes and follows the ID-pattern

I have a VBA RegEx function that searches for a ID with a pattern of 1 letter followed by 6 digits. I've been using the pattern [a-zA-Z]{1}\\d{6} to find the IDs but am absolutely stuck on special cases.

Typically, the IDs are surrounded by non-character/numbers like "domain\\J123456, Last login [etc]" so I tried [^a-zA-Z]{1}[a-zA-Z]{1}\\d{6}[^a-zA-Z\\d]{1} which kinda-worked. Where this pattern-string fails is

  • When the ID is at the beginning of the string: "J123456, Last login [etc]"
  • When the ID is at the end of the string: "domain\\J123456"
  • When the ID is the whole string: "J123456"

A further frustration is that VBA's implementation of RegEx is less than compliant and sometimes uses non-standard expressions. I know I could do multiple RegEx searches but that seems inefficient. Any help from The Gods would be greatly appreciated.

Specs: I'm using MS Access 2007 and the "Microsoft VBScript Regular Expressions 5.5" library (and my environment is locked down so I can't introduce new/different libraries into the mix).

EDIT : Here is some mocked up entries that present some of the scenarios. I'll bold what should be a match. There will only ever be 1 valid match per entry.

  • Sid="S-21-121X4440339-682Z003330" Name="Smith, John" LoginName="NAEAST\\ "
  • J123456 Key=B214440339-1979792683-682E003330
  • L398938943-090934989 By J123456
  • J123456

EDIT - The Solution

Based on the accepted answer below, here is the working function for anyone who needs it.

Public Function findID(search_str As String)
  '  Find [letter][6-digit number] ID in string
  Dim regEx As New VBScript_RegExp_55.RegExp
  Dim matches

  findSID = ""
  regEx.Pattern = "(?:^|[^a-zA-Z\d])([a-zA-Z]{1}\d{6})(?![a-zA-Z\d])"
  regEx.Global = True
  If regEx.test(search_str) Then
    Set matches = regEx.Execute(search_str)
    '-- Some of the matches return the match and left-most border-char.  Trim it off
    findID = Right(matches(0).Value, 7)  
   Else
    findID = "No match"
    End If

End Function

You use a regex of the form

(?:^|[^a-zA-Z\d])([a-zA-Z]{1}\d{6})(?![a-zA-Z\d])
  • The capture group 1 will contain the ID

  • (?![a-zA-Z\\d]) asserts that the regex is not followed by digits or alphabets

Regex Example

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