Select to view content in your preferred language

Regex to allow both ' and "

506
6
Jump to solution
02-26-2024 09:40 AM
CoreyWilliamsESRIAccount
New Contributor II

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\{\}\[\]\^\`\~\;\:\|\¿]*$")

0 Kudos
1 Solution

Accepted Solutions
abureaux
MVP Regular Contributor

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.

View solution in original post

0 Kudos
6 Replies
CodyPatterson
Regular Contributor

Hey @CoreyWilliamsESRIAccount 

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

0 Kudos
CoreyWilliamsESRIAccount
New Contributor II

Hi @CodyPatterson ,

I believe it is trying to terminate the formula at the " which is generating the below message.

CoreyWilliamsESRIAccount_0-1708971077400.png

 

0 Kudos
abureaux
MVP Regular Contributor

Toss it into a regex viewer (e.g., https://regexr.com/) and you can see:

abureaux_0-1709048794538.png

Toss in another "\" does the trick

abureaux_1-1709048836677.png

 

^[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.

0 Kudos
CoreyWilliamsESRIAccount
New Contributor II

@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".

0 Kudos
abureaux
MVP Regular Contributor

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.

0 Kudos
CoreyWilliamsESRIAccount
New Contributor II

Thank you! The following allowed it work.

regex(.,"^[a-zA-ZÀ-Ÿ0-9\. _\-\,\?\/\!\@\#\$\%\&\*\(\)\+\=\'\\\<\>\n\{\}\[\]\^\`\;\:\|\¿\x22]*$")