0

I am working on categorizing strings, and thought the RegExp could be the right tool for it.

The kind of strings I want to separate are of the following appearance:

@1|27|23|02|12
@1|27|23|02|01|@ZZZ
@1|27|23|02|08|0152829
@1|27|23|02|01|06180704

I want to be able to categorize each of these strings. Thats what I started with:

Function ColumnValue(RefColObject, ColumnObject, BaseRowIndex, BaseColumnIndex)

        SAPString = RefColObject.SystemFullName
        
        Msgbox SAPString
        
        Set re = New RegExp
        With re
                .Pattern = "\@[0-9]\|[0-9][0-9]\|[0-9][0-9]\|[0-9][0-9]"
                .IgnoreCase = False
                .Global = False
        End With
        
        Set re2 = New RegExp
        With re2
                .Pattern = "\@[0-9]\|[0-9][0-9]\|[0-9][0-9]\|[0-9][0-9]\|[0-9]{7,8}"
                .IgnoreCase = False
                .Global = False
        End With
        
        If re2.Test(SAPString) Then
                Msgbox "Mit SAP"
                
        ElseIf re.Test(SAPString) Then
                Msgbox "Nur Pfad"
                
        End If
        
        Set re = Nothing
        
        
End Function

My problems/questions..

When I test the string @1|27|23|02|01|06180704 with my RegExp re it says true of course, can I tell the RegExp that after \@[0-9]\|[0-9][0-9]\|[0-9][0-9]\|[0-9][0-9] there should not be anything more?

When I test my RegExp re2 with the string @1|27|23|02|01|06180704, I don't get a true back. In my understanding the last part of my RegExp re2 [0-9]{7,8} should say that in the end of the string there must be a string of 7-8 numbers from 0-9. Where did I go wrong with my considerations here?

Thank you for your help community!!

InSync
  • 4,851
  • 4
  • 8
  • 30
Luca
  • 17
  • 5
  • 1
    You have three `\|[0-9][0-9]` in the regex but 4 such instances in the text (`|27|23|02|01`). Consider using a quantifier instead of repeating: [`@[0-9](?:\|[0-9]{2}){4}\|[0-9]{7,8}`](https://regex101.com/r/nhi2s5/1) (or, trickier: [`@[0-9](?:\|[0-9]{2}){5}[0-9]{5,6}`](https://regex101.com/r/nhi2s5/2)) – InSync Aug 10 '23 at 06:19
  • 1
    What about `$` to match end of string? – selbie Aug 10 '23 at 06:20
  • @InSync Thank you for your input, that was a pretty bad failure.. Thanks! I tried working with the qantifier you mentioned, but so far I was not able to get the true back with it. Im wondering why you did the ":"? I understand it should repeat the 2 digits with the "|" 4 times, but so far I didn't found the false part.. – Luca Aug 10 '23 at 07:34
  • @Selbie Thanks to you also, that was very useful! – Luca Aug 10 '23 at 07:34
  • `(?:)` is a [non-capturing group](https://stackoverflow.com/q/3512471). Your regexes *should* work once they have `$` at the end, as suggested by @selbie. Unfortunately, I don't know VB, so I can't really help you with the rest of the script. – InSync Aug 10 '23 at 12:01

0 Answers0