validate period delimited numbers regex
I have a tricky regex.
- numbers are is delimited by periods .
- there can be a max of 4 p开发者_JAVA技巧eriods. between each period there can be 0 to 4 numbers.
valid expressions include
....
...
..
.
.1234.1234
..1234.1234.1234
1234.1234.1234.1234.1234 (this is the max string)
1234.1234
1234....1234
1234.1234.1234
invalid strings include the following
12345 (too many digits example)
..1234...1234 (this is 5 periods)
1234.12345 (too many digits example)
Thanks in advance
I don't think it's particularly tricky... Just repeat a group of period+numbers:
^\d{0,4}(\.\d{0,4}){0,4}$
Edit: Works better with correct syntax. What's this for btw?
Edit 2: Depending on your regex flavor you may need ^$
.
/^\d{0,4}(?!\d)(?:\.?\d{0,4}(?!\d)){0,4}$/
Tested in JS:
var r = /^\d{0,4}(?!\d)(?:\.?\d{0,4}(?!\d)){0,4}$/;
r.test('....'); // true
r.test('...'); // true
r.test('..'); // true
r.test('.'); // true
r.test('.1234.1234'); // true
r.test('..1234.1234.1234'); // true
r.test('1234.1234.1234.1234.1234'); // true
r.test('1234.1234'); // true
r.test('1234....1234'); // true
r.test('1234.1234.1234'); // true
r.test('12345'); // false
r.test('..1234...1234'); // false
r.test('1234.12345'); // false
精彩评论