Home > FAQ, How To's > How To: Introduction to Regular Expressions (RegEx)

How To: 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 IsRegEx, 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,})$

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:
!IsRegEx(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!

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.

That’s it!

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

Categories: FAQ, How To's
  1. September 17, 2012 at 6:30 am

    I was told that RegEx is not implemented on Android (I guess, Dooblo has to write it in big red letters under the header:), so I have to use JavaScript functions instead http://www.w3schools.com/jsref/jsref_obj_regexp.asp
    (I’ve implemented simple check for “numbers only” field in open choice grid and it worked fine on Bliss Pad and did not work on Samsung Tablet)

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: