Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regexp to match xxx.xxx.xxx?

Tags:

regex

php

I need to check input with preg_match, which must be of this format: xxx.xxx.xxx The number of block can vary... These are all examples of valid inputs:

001
00a.00a
0fg.001
aaa.aaa.001
001.001.002.001.001.001

Well I could probably write a regexp something like:

^([\da-z]{3}\.?)+$

But here comes the problem with the quantifier of the period. I mean if I use '?' to match 0 or 1 times, it would also match even if skip the dots somewhere, eg:

000.001.0010az001

then, if I used {1} to match one time, it would match nothing, because the last block does not have a dot.

So I can't think what to think of... Please advice

like image 820
Anonymous Avatar asked Jan 27 '26 14:01

Anonymous


1 Answers

You can use:

^[\da-z]{3}(?:\.[\da-z]{3})*$
like image 90
codaddict Avatar answered Jan 29 '26 04:01

codaddict



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!