Regex for dimensions (length x width x height)

1684
3
Jump to solution
11-04-2022 01:39 PM
ZachBodenner
MVP Regular Contributor

Hello,

I'm looking for a regex expression that would restrict a text entry to length x width x height style dimensions. Ideally, no limits to the number of digits available in the number part of the dimensions, x can be either lower or upper case. 

Not super well versed in regex. I've managed to find good expressions from ESRI to get regex for email input to work , but I'm struggling with this one. I've used this stack overflow threat as reference but none have been successful:

https://stackoverflow.com/questions/39452200/match-product-dimensions-with-regular-expression

0 Kudos
1 Solution

Accepted Solutions
JamesTedrick
Esri Esteemed Contributor

Hi @ZachBodenner ,

The basic pattern you're looking for has the following components in sequence:

  1. Some amount of number characters (at least 1 character and possibly with the appropriate decimal separator for you region)
  2. ' x ', where the spaces may be optional
  3. More number characers
  4. Another ' x '
  5. More number characters

Each of those parts has a corresponding part of the overall regex pattern.  I'm going to assume a period (.) is the appropriate decimal separator in the example below:

  1. A decimal number: (\d+\.?\d*)
    1. ( ) group everything together
    2. \d is a digit character
    3. + is used to match 1 or more numeric characters
    4. \. matches a period (which is a special character in regex and needs the \ to make it match as a character)
    5. ? is used to match 0 or 1 instance of the period (so 2 periods in a row will fail the match)
    6. \d matches number characters (the decimal portion)
    7. * matches 0 or more numeric characters (so that integers without decimals are supported)
  2. ' x ': (\s?x\s?)
    1. ( ) groups everything together
    2. \s matches a white space character (like space)
    3. ? is used to match 0 or 1 instance of the white space character (so 2 spaces in a row will fail the match)
    4. x is the character 'x'

Placing them together to get:

(\d+\.?\d*)(\s?x\s?)(\d+\.?\d*)(\s?x\s?)(\d+\.?\d*)

 

 

View solution in original post

0 Kudos
3 Replies
JamesTedrick
Esri Esteemed Contributor

Hi @ZachBodenner ,

The basic pattern you're looking for has the following components in sequence:

  1. Some amount of number characters (at least 1 character and possibly with the appropriate decimal separator for you region)
  2. ' x ', where the spaces may be optional
  3. More number characers
  4. Another ' x '
  5. More number characters

Each of those parts has a corresponding part of the overall regex pattern.  I'm going to assume a period (.) is the appropriate decimal separator in the example below:

  1. A decimal number: (\d+\.?\d*)
    1. ( ) group everything together
    2. \d is a digit character
    3. + is used to match 1 or more numeric characters
    4. \. matches a period (which is a special character in regex and needs the \ to make it match as a character)
    5. ? is used to match 0 or 1 instance of the period (so 2 periods in a row will fail the match)
    6. \d matches number characters (the decimal portion)
    7. * matches 0 or more numeric characters (so that integers without decimals are supported)
  2. ' x ': (\s?x\s?)
    1. ( ) groups everything together
    2. \s matches a white space character (like space)
    3. ? is used to match 0 or 1 instance of the white space character (so 2 spaces in a row will fail the match)
    4. x is the character 'x'

Placing them together to get:

(\d+\.?\d*)(\s?x\s?)(\d+\.?\d*)(\s?x\s?)(\d+\.?\d*)

 

 

0 Kudos
ZachBodenner
MVP Regular Contributor

Hey, this is great, that explanation is so helpful! The only problem is that Survey123 Connect is giving me the below error:

ZachBodenner_3-1667830282009.pngZachBodenner_4-1667830303459.pngZachBodenner_5-1667830322471.png

 

 

Here's the xls form with the regex in the constraint column:

ZachBodenner_6-1667830339374.png

 

 

 

0 Kudos
ZachBodenner
MVP Regular Contributor

Update: it worked when I structured it this way:

regex(., '^(\d+\.?\d*)(\s?x\s?)(\d+\.?\d*)(\s?x\s?)(\d+\.?\d*)')

I used the email regex formatting as reference and added (.,'^ to the beggining.

Sidenote, what would my syntax be if I wanted the "x" to be either caps or lower case? The current format seems to mandate lower case.

 

0 Kudos