0

I need to check input value:

  • x > 0; x <= 8,00
  • maximum 2 digits after comma.

My regex = 8,0|8,00|[1-8]{1}|([0-7]{1,1},[0-9]{1,2})

[1-8]{1}: when users type only 1 2 3 4 .... 8
[0-7]{1,1}: only one character from 0 to 7 before comma
[0-9]{1,2}: maximum 2 digits after comma

I type 1,23412344 and it is still valid. Could you point out my mistake in this pattern and correct for me ?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
songvan
  • 369
  • 5
  • 21
  • That allows you to type in 0 or 0,0 or 0,00 all of which are not > 0. – juharr Jul 21 '16 at 16:56
  • 1
    For validation, I believe it's this `^(?:8(?:,0{0,2})?|[1-7](?:,[0-9]{0,2})?|0?,(?:0[1-9]|[1-9][0-9]?))$` but could be wrong. Hard to see this answer in the Stribnerz duplicate though. –  Jul 21 '16 at 17:30
  • thank you guys. @sln: your solution is great, it works well. But actually I don't understand your answer. ^....$ is for comparing the exact thing inside. But I don't understand "?" and its position, sometimes it is before ":" sometimes it is before ",". Sometimes it is after ")", sometimes it is before ")". Could you please explain each part (between | |) for me ? – songvan Jul 21 '16 at 21:00
  • 1
    @amateur - It's just the permutations separated by alternations. Install [RegexFormat](http://regexformat.com), cut 'n paste the regex into it, then press the format button.. all things become exposed. I would dump the formatted regex here but your post is locked. –  Jul 21 '16 at 21:15
  • Test comment: `^ (?: 8 (?: , 0{0,2} )? | [1-7] (?: , [0-9]{0,2} )? | 0? , (?: 0 [1-9] | [1-9] [0-9]? ) ) $` –  Jul 21 '16 at 21:18
  • thanks a lot @sln, I did not know RegexFormat before. Now the problem is much more clearer :). There is only one thing I still don't understand. That is `?:`. For example `n?` is for testing the matching the 0 or 1 occurence of `n`. But `?:` is for what, I tried to delete it in your regex in RegexFormat and I see the error like # (1 start), #2, #3, #4, # (1 end). I tried to read in internet but still not understand `?:`. Could you please give me a small and short example about `?:` ? – songvan Jul 22 '16 at 08:08
  • Ah I found the same question about `?:` here already :) – songvan Jul 22 '16 at 11:25
  • @amateur - For the basics, there are a lot of tutorials out there. Quick answer is _regex_ has parenthesis syntax `(xx...)` that defines and scopes a sub expression. The syntax `xx` further defines what the parens mean. If `xx == '?:'` it is a non-capture cluster group. If `xx == '|'` it is branch reset, if `xx == 'smix-smix'` it is an inline-modifier. If `xx == '=' or '!' or '<=' or '<!'` it's an assertion, Etc... –  Jul 22 '16 at 17:46

0 Answers0