Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a string EXACTLY matches a regex pattern?

Tags:

regex

php

I'm working on a registration script for my client's product sales website.

I'm currently working on a reference ID input area, and I want to make sure that the reference ID is within the correct parameters of the payment method

The Reference ID will look something like this: XXXXX-XXXXX-XXXXX

I'm trying to use this RegEx pattern to match it: /(\w+){5}-(\w+){5}-(\w+){5}/

This matches it perfectly, but it also matches XXXXX-XXXXX-XXXXXXXXXX

Or at least it finds a match in there. I want it to make sure the entire string matches. I'm not too familiar with RegEx

How can I do this?

like image 887
Rob Avatar asked Dec 01 '22 08:12

Rob


1 Answers

You need to use start and finish anchors. Alternatively, if you don't need to capture those groups, you can omit the parenthesis.

Also, the +{5} means match more than once exactly 5 times. I believe you didn't want that so I dropped the +.

/^\w{5}-\w{5}-\w{5}\z/

Also, I used \z so your string doesn't match "abcde-12345-edcba\n".

like image 197
alex Avatar answered Dec 06 '22 15:12

alex