Introduction to Regular Expressions (RegEx)

Overview

Regular expressions are patterns of characters which are used in order to check if some text is matched to your requests.
A common known usage of regular expression is when demanding a valid email address in a free text question – regular expressions allows you to validate if the input is a valid email address.

RegEx Quick Reference


How To Do It?

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 a RegEx I took from the internet 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)).

Just for my convenience, I have placed it inside a global variable, so I don’t need to write the whole expression when I wish to use it.
*Here is a ‘How-To’ about Global Variables

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

You can implement this global variable wherever you want in your survey.
Now, I would like to check if the answer of my Open Ended question which asks for the respondents email, is a valid email address.

I have created the following Validation Rule:
!dblIsRegEx(Vars["emailRegEx"],Answer(CurrQues))

This basically checks if the RegEx is NOT valid.
And have configured it to prompt if the email is not valid.

And as you can see, I have entered a not valid email address, and the validation RegEx rule has saved the day!

 

IMPORTANT NOTES:
- When writing your Regular Expression, please note that due to Scripting global syntax rules, when needing to write a back-slash '\', it should be written twice, like this: '\\' , in order for it to recognize it as a single back-slash - writing a single back-slash will not work. 

Other Usages Of RegEx?

Basically RegEx can come in hand in a vast range of ways, for example:
- Validating a certain code which has to match a certain pattern (like Bar-Codes, Policy-Codes, etc’).
- Validating a certain pattern of phone number.
- And any pattern of text you have in mind.

There is a lot of info about RegEx in the internet, as this is a whole language for validating text patterns.
For quick reference of the language syntax and testing your regular expressions on your text, I would recommend using the following site:

http://rubular.com/

It has a RegEx validation tool and a quick reference for RegEx syntax.

You can find a few examples in this sample survey: http://support.dooblo.net/entries/31759632-Regular-Expressions-examples

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.