Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique RegEx string

Tags:

.net

regex

I have a set of requirements which state that the input must be validated as follows:

  • Be between 1 and 7 characters
  • Numeric
  • Greater than 0
  • and allow a decimal point

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

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

like image 339
Paddy Avatar asked Dec 06 '22 02:12

Paddy


1 Answers

We can think this way:

^(?=\d+(?:\.\d+)?$)(?![0\.]+$).{1,7}$
  • Positive lookahead from the beginning: it should be composed of digits and optional . symbol (number pattern)
  • Negative lookahead from the beginning: it shouldn't be composed only of 0 and . symbols (pattern to exclude 0 value).
  • It should have between 1 and 7 symbols.

Regular expression visualization

like image 90
Ulugbek Umirov Avatar answered Dec 19 '22 22:12

Ulugbek Umirov