I am using Survey123 Connect and attempting to write a Regex formula to only allow certain characters. However, I am running into an error when trying to allow both ' and " in my formula. Does anyone have a workaround?
regex(.,"^[a-zA-ZÀ-Ÿ0-9\. _\-\,\?\/\!\@\#\$\%\&\*\(\)\+\=\'\\\<\>\n\{\}\[\]\^\`\~\;\:\|\¿]*$")
Solved! Go to Solution.
Okay. So the issue is the quotes used. (I know, obvious. But I'm going somewhere).
If you enclose you regex in single quote, then single quotes will give you issues. If you enclose it in double quotes, then double quotes will give you issues. Both variants are valid. Since we are currently talking about double quotes, we will leave the expression enclosed in double quotes.
This is what we have now. Technically not wrong as it parses in RegExr, but S123 doesn't like it (for the aforementioned reason).
regex(.,'^[a-zA-ZÀ-Ÿ0-9\. _\-\,\?\/\!\@\#\$\%\&\*\(\)\+\=\'\\"\\\<\>\n\{\}\[\]\^\`\~\;\:\|\¿]*$')
We can actually relapse " with its hex code of x22 (single quote is x27, if you are going that route).
This parses in S123 and will restrict the desired characters.
regex(.,'^[\x22\x27a-zA-ZÀ-Ÿ0-9\. _\-\,\?\/\!\@\#\$\%\&\*\(\)\+\=\\\<\>\n\{\}\[\]\^\`\~\;\:\|\¿]*$')
I'm not very good at regex(), so this is likely not optimal. But it seems to work at least!
EDIT: made a copy-paste error. Too many tests... I just added both the single and double quotes to the list and it works now.
Would this work for you? You may need to double escape the double quotes:
^[a-zA-ZÀ-Ÿ0-9\. _\-\,\?\/\!\@\#\$\%\&\*\(\)\+\=\'\\"\\\<\>\n\{\}\[\]\^\`\~\;\:\|\¿]*$
If it doesn't end up working, what does the error state when entering?
Hope that helps!
Cody
Hi @CodyPatterson ,
I believe it is trying to terminate the formula at the " which is generating the below message.
Toss it into a regex viewer (e.g., https://regexr.com/) and you can see:
Toss in another "\" does the trick
^[a-zA-ZÀ-Ÿ0-9\. _\-\,\?\/\!\@\#\$\%\&\*\(\)\+\=\'\\\"\\\<\>\n\{\}\[\]\^\`\~\;\:\|\¿]*$
Creating an "allow list" in regex" can be annoying. Creating a "deny list" is typically easier. Something to think about.
@abureaux Unfortunately I am receiving the same error message when inputting the recommended into the Survey123 Connect XLSForm. It appears Survey123 stops reading the formula at " regardless if there's an escape character before it.
I did previous try a "deny list" however ran into issues where regexr.com would pass but it would not in Survey123. Instead, it was recommended to create an "allow list".
Okay. So the issue is the quotes used. (I know, obvious. But I'm going somewhere).
If you enclose you regex in single quote, then single quotes will give you issues. If you enclose it in double quotes, then double quotes will give you issues. Both variants are valid. Since we are currently talking about double quotes, we will leave the expression enclosed in double quotes.
This is what we have now. Technically not wrong as it parses in RegExr, but S123 doesn't like it (for the aforementioned reason).
regex(.,'^[a-zA-ZÀ-Ÿ0-9\. _\-\,\?\/\!\@\#\$\%\&\*\(\)\+\=\'\\"\\\<\>\n\{\}\[\]\^\`\~\;\:\|\¿]*$')
We can actually relapse " with its hex code of x22 (single quote is x27, if you are going that route).
This parses in S123 and will restrict the desired characters.
regex(.,'^[\x22\x27a-zA-ZÀ-Ÿ0-9\. _\-\,\?\/\!\@\#\$\%\&\*\(\)\+\=\\\<\>\n\{\}\[\]\^\`\~\;\:\|\¿]*$')
I'm not very good at regex(), so this is likely not optimal. But it seems to work at least!
EDIT: made a copy-paste error. Too many tests... I just added both the single and double quotes to the list and it works now.
Thank you! The following allowed it work.
regex(.,"^[a-zA-ZÀ-Ÿ0-9\. _\-\,\?\/\!\@\#\$\%\&\*\(\)\+\=\'\\\<\>\n\{\}\[\]\^\`\;\:\|\¿\x22]*$")
Hello,
Could someone please help with similar problem, I have a Survey123 Connect XLSForm, where I am capturing the First and Last Name of the user by asking them to type it in the answer. I don't have complicated international names, I only need to capture apostrophe in the last name, example: Joe O'Smith.
Here is my regular expression entered in the Constraint Column:
regex(., '^[A-Za-z][A-Za-z]+([\ A-Za-z][A-Za-z\']+)*')
The Survey123 Connect does not accept this expression, i did check it in the regex101 builder, I am assuming it has to do with the ' apostrophe as in the question above. (?)
Thank you!