Unique RegEx string

I have a set of requirements that claim that the input should be verified as follows:

  • 1 to 7 characters
  • Numeric
  • More than 0
  • and allow decimal point

I currently have ^(?!0{1,7}$)(\d+(\.\d)?){1,7}$

But this does not limit the total string to a maximum of 7 characters.

+4
source share
3 answers

We can think like this:

^(?=\d+(?:\.\d+)?$)(?![0\.]+$).{1,7}$
  • A positive look from the very beginning: it should consist of a number and an optional character .(number template)
  • A negative look from the very beginning: it should not consist only of 0and .(a template to exclude the value 0).
  • It must have 1 to 7 characters.

Regular expression visualization

+8
source

Try this regex:

^(?!0\.0$)(?:[1-9]|(?<=[1-9]|\.)0|0?\.(?=\d+)){1,7}$
  • 1 9 ().
  • 0 :
    • .
    • .
  • 7 .

Regular expression visualization

Demo

http://regex101.com/r/kI2gB9

+4

I think this will do the trick:

^(?!0*(\.0*)?$)(?=.{1,7}$)\d+(\.\d+)?$

test: http://jsfiddle.net/29jR6/1/

+3
source

Source: https://habr.com/ru/post/1535918/


All Articles