Regex: match feet and/or inches -


i'm trying match feet , inches can't manage , "and/or" if first half correct validates:

code: (in javascript)

var pattern = "^(([0-9]{1,}\')?([0-9]{1,}\x22)?)+$";  function testing(input, pattern) {         var regex = new regexp(pattern, "g");         console.log('validate '+input+' against ' + pattern);         console.log(regex.test(input));     } 

acceptable answers should be: 1' , 1'2" , 2" or 2 (assumes inches)

not acceptable: else including empty

but regex accepts 1'1'

remove + @ end (which allows more 1 instance of feet/inches right now) , check empty string or illegal entries 1'2 using separate negative lookahead assertion. i've changed regex group 1 contains feet , group 2 contains inches (if matched):

^(?!$|.*\'[^\x22]+$)(?:([0-9]+)\')?(?:([0-9]+)\x22?)?$ 

test live on regex101.com.

explanation:

^          # start of string (?!        # assert following can't match here:  $         # end of string marker (excluding empty strings match) |          # or  .*\'      # string contains '  [^\x22]+  # if follows doesn't include "  $         # until end of string (excluding invalid input 1'2) )          # end of lookahead assertion (?:        # start of non-capturing group:  ([0-9]+)  # match integer, capture in group 1  \'        # match ' (mandatory) )?         # make entire group optional (?:        # start of non-capturing group:  ([0-9]+)  # match integer, capture in group 2  \x22?     # match " (optional) )?         # make entire group optional $          # end of string 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -