i trying create regular expression matches following:
- one or more digits
- allow 0 1 period after first digits
- if period present
- require 1 - 2 digits after period
here regex have far, doesn't work cases:
/\d{1,}\.{0,1}\d{1,2}/
all of these test cases should pass
1.9 1 12 1211.1 121234.14
all of these test cases should not pass
z z1 z.5 z.55 # no letters .9 # required 1 or more digits before period if period present 34. # required 1-2 digits after period if period present 4..3 4..55 # 1 period 4.333 # 1-2 digits after period 111,222.44 # no comma
edited
i think resolve..
/^\d{1,}(\.\d{1,2}){0,1}$/
my test case:
2.3.0 :129 > regex = /^\d{1,}(\.\d{1,2}){0,1}$/ => /^\d{1,}(\.\d{1,2}){0,1}$/ 2.3.0 :161 > regex.match("1.9") => #<matchdata "1.9" 1:".9"> 2.3.0 :162 > regex.match("1") => #<matchdata "1" 1:nil> 2.3.0 :163 > regex.match("12") => #<matchdata "12" 1:nil> 2.3.0 :164 > regex.match("1211.1") => #<matchdata "1211.1" 1:".1"> 2.3.0 :165 > regex.match("121234.14") => #<matchdata "121234.14" 1:".14"> 2.3.0 :166 > regex.match("z") => nil 2.3.0 :167 > regex.match("z1") => nil 2.3.0 :168 > regex.match("z.5") => nil 2.3.0 :169 > regex.match("z.55") => nil 2.3.0 :170 > regex.match(" .9") => nil 2.3.0 :171 > regex.match("34.") => nil 2.3.0 :172 > regex.match("4..3") => nil 2.3.0 :173 > regex.match("4..55") => nil 2.3.0 :174 > regex.match("4.333") => nil 2.3.0 :175 > regex.match("111,222.44") => nil
Comments
Post a Comment