I try to regex match string like these examples:
NetTAP IPX C-4-16
SmartTAP DP6409 Dual E1-11-16
but not these ones:
NetTAP IPX C-4-16-0
SmartTAP DP6409 Dual E1-11-16-0
SYSTEM
I've tried some solutions without success.
My idea was to search the -\d\d 2 times but not 3 times
(?!\-\d )(\-\d ){2}$
or
[^(\-\d )](\-\d ){2}$
Could you help me?
CodePudding user response:
You can use
(?<!-\d )(?:-\d ){2}$
See the regex demo. Details:
(?<!-\d )
- a negative lookbehind that fails the match if there is a-
char and one or more digits immediately to the left of the current location(?:-\d ){2}
- two repetitions of-
and one or more digits$
- end of string.
CodePudding user response:
You can also match 3 times what you want to avoid, and capture 2 times what you want to keep using a capture group.
(?:-\d ){3}$|(-\d -\d )$
The pattern matches:
(?:-\d ){3}$
Match 3 repetitions at the end of the string|
Or(-\d -\d )$
capture 2 repetitions at the end of the string in group 1