Help with regex syntax in constraint

166
2
Jump to solution
02-21-2022 04:10 AM
DOE_-_DavidTipping
New Contributor II

Hi - I'm trying to come up with a constraint using the regex function. 

I need to restrict field input to fit with a simple alphanumeric 1km grid with letters columns from A to R along the top and numbered rows of 1 to 11. 

The constraint format is a capital letter between A and R followed by a 2 digit number between 01 and 11, so that A01 and  R11 pass but A2 or A12 or a2 or S11 all fail.

I have tried many combinations but not found a workable solution. 

regex (., '^[A-R]{1}[01-11]{2}$') passes the test in Survey123 connect but doesn't work when published. regex (., '^[A-R]{1}\d[01-11]{2}$') fails

Any thoughts - should this be done through an input mask instead?

Many thanks

David Tipping

 

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Notable Contributor

Regex 101 is a great resource for writing and testing regex.

I notice one issue with your expression, which is the "[01-11]" bit. Regex 101 gives the explanation:

Match a single character present in the list below
[01-11]
0 matches the character 0 with index 4810 (3016 or 608) literally (case sensitive)
1-1 matches a single character in the range between 1 (index 49) and 1 (index 49) (case sensitive)
1 matches the character 1 with index 4910 (3116 or 618) literally (case sensitive

To match the numbers 0 - 11 with a padded zero, we can write "(0[1-9]|1[01])". This will match a 0 followed by anything from 1 to 9, or a 1 followed by a 0 or a 1.

Secondly, there's really no need to have the {1}, as regex will by default only match your bracketed range of characters once. So the full expression would be

[A-R](0[1-9]|1[01])

You can see that those other cases all fail to match:

jcarlson_0-1645456189712.png

 

- Josh Carlson
Kendall County GIS

View solution in original post

2 Replies
jcarlson
MVP Notable Contributor

Regex 101 is a great resource for writing and testing regex.

I notice one issue with your expression, which is the "[01-11]" bit. Regex 101 gives the explanation:

Match a single character present in the list below
[01-11]
0 matches the character 0 with index 4810 (3016 or 608) literally (case sensitive)
1-1 matches a single character in the range between 1 (index 49) and 1 (index 49) (case sensitive)
1 matches the character 1 with index 4910 (3116 or 618) literally (case sensitive

To match the numbers 0 - 11 with a padded zero, we can write "(0[1-9]|1[01])". This will match a 0 followed by anything from 1 to 9, or a 1 followed by a 0 or a 1.

Secondly, there's really no need to have the {1}, as regex will by default only match your bracketed range of characters once. So the full expression would be

[A-R](0[1-9]|1[01])

You can see that those other cases all fail to match:

jcarlson_0-1645456189712.png

 

- Josh Carlson
Kendall County GIS
DOE_-_DavidTipping
New Contributor II

Thanks Josh - It worked a treat

Love the Regex 101 resource, I will pass it to other users

Many thanks

David Tipping

0 Kudos