Introduction to Regular Expressions (RegEx)

Overview

Regular expressions are patterns of characters that can be used in order to check if some text is matching your requested pattern.

Few examples to the usage of regular expressions is validation a phone number pattern, an identification code, zip codes etc. that are entered by the surveyor/respondent during an interview as text in open ended questions.

 

How To use regular expressions?

SurveyToGo has a built-in function called dblIsRegEx.

This function gets two parameters:
1st parameter is the RegEx expression.
2nd parameter is the text to check.

This function returns ‘true’ if the text is valid according to the RegEx expression, or ‘false’ if the text is not valid according to the RegEx expression.

Let’s look on the email validation example.

Here is an example RegEx for validating email addresses:

^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$

Please note: this is only an example, we have already a predefined function that validates the email address called !IsEMail(Answer(CurrQues)).

We would like to check if the answer of anOpen Ended question which asks for the respondents email, is a valid email address.

We will use the following Validation Rule condition:
!dblIsRegEx("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$",Answer(CurrQues))


And as you can see, if we enter a not valid email address the following message will appear and the interview will not be allowed to proceed.


IMPORTANT NOTE:
When writing your Regular Expression, please note that as we pass it as a string parameter to the function, when needing to write a back-slash '\' as part of he rule, it should be written twice, like this: '\\'.

So for example to have a rule where you allow only non digit characters to be entered, in Rubular you would write it as ^\D$, but in the validation rule you would write it as:

!dblIsRegEx("^\\D$", Answer(CurrQues));

For quick reference of the syntax and testing your regular expressions we recommend using the following site:

http://rubular.com/

You can find a few examples in this sample survey: https://support.dooblo.net/hc/en-us/articles/208296435-Regular-Expressions-examples


RegEx Quick Reference

We placed here a screenshot with the most common regular expression operators for a quick reference (the below is taken from the Rubular site):

That’s it!

Visit us at: http://www.dooblo.net

Was this article helpful?
1 out of 1 found this helpful
Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.