Reputation: 6759
I've tried several patterns and fiddling with some pattern from Capture word between optional hyphens regex, Regular Expressions: How to find dashes between words, What's the difference between "(\w){3}" and "(\w{3})" in regex? and also read Reference - What does this regex mean?
My best attempt so far was:
(\w{3}\-)
with test data:
THU-abs-sss-ddd
012-aa-aaa-aaa
which match:
Despite what I would like to achieve is an exact pattern validation against: XXX-XXX-XXX-XXX where XXX is 3 alphanumeric and dash repeated 3 times and closed with another XXX alphanumeric.
I've also tried using (\w{3}\-)(\w{3})
but then the result was:
What am I missing to complete the pattern?
Upvotes: 2
Views: 51
Reputation: 370619
You need to repeat the \w{3}-
group 3 times:
(?:\w{3}-){3}\w{3}
(note that -
doesn't need to be escaped, and that you should use non-capturing groups unless you really need to capture)
Upvotes: 2