Reputation: 61
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?
Upvotes: 2
Views: 342
Reputation: 163217
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 1Upvotes: 0
Reputation: 626738
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.Upvotes: 2