Regular Expression in a Script

Answered Question
Dec 16th, 2009

Does anybody have a syntax example they could share of a regular expression as used in a script?  I can figure out the regular expression syntax, I'm just stumped on how to deal w/ it in a script.  I think I just have a parentheses or quote issue going on.

What I'm trying to achieve is a very simple validation of a proper telephone number entry.  If entered telephone number is valid, then move forward, if not, return an error and have caller re-enter.  In its over-simplified form (looking for a 7 digit number), I think it would look something like this in the expression editor:

strCallBackNum == \\d{7}

Best,

J

I have this problem too.
0 votes
Correct Answer by Anthony Holloway about 6 years 11 months ago

Jon's solution is a simple yet powerful display of input validation.  Since you also asked about using regex, here is a code snippet that I use to validate caller input when dealing with phone numbers.

Do {

     java.util.ListIterator filterArrayListIterator = (

          new java.util.ArrayList(

               java.util.Arrays.asList(phoneNumberFilters)

          )

     ).listIterator();


     while (filterArrayListIterator.hasNext()) {

          matchedPattern = (String) filterArrayListIterator.next();

          isValidPhoneNumber = (

               (

                    java.util.regex.Pattern.compile(matchedPattern)

               ).matcher(phoneNumber)

          ).matches();

          if (isValidPhoneNumber) {

               break;

          } else {

               matchedPattern = null;

          }

     }

}

It works off the following variables (I'll even provide some test data):

boolean isValidPhoneNumber = false;

String phoneNumber = "3966";

String matchedPattern = null;

String[] phoneNumberFilters = new String[] {"12..", "39[1-6].", "590[^0]"};

You can have only filter, or a hundred, it's pretty flexible, and it resembles dial peer pattern matching quite nicely.

If you had a script with just this one line in it, you could call it via a Subflow, passing the phoneNumber to test, and have your self a handy centralized phone number validation tool which any script could use.

  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 5 (4 ratings)
Loading.
Correct Answer
Anthony Holloway Wed, 12/16/2009 - 10:24

Jon's solution is a simple yet powerful display of input validation.  Since you also asked about using regex, here is a code snippet that I use to validate caller input when dealing with phone numbers.

Do {

     java.util.ListIterator filterArrayListIterator = (

          new java.util.ArrayList(

               java.util.Arrays.asList(phoneNumberFilters)

          )

     ).listIterator();


     while (filterArrayListIterator.hasNext()) {

          matchedPattern = (String) filterArrayListIterator.next();

          isValidPhoneNumber = (

               (

                    java.util.regex.Pattern.compile(matchedPattern)

               ).matcher(phoneNumber)

          ).matches();

          if (isValidPhoneNumber) {

               break;

          } else {

               matchedPattern = null;

          }

     }

}

It works off the following variables (I'll even provide some test data):

boolean isValidPhoneNumber = false;

String phoneNumber = "3966";

String matchedPattern = null;

String[] phoneNumberFilters = new String[] {"12..", "39[1-6].", "590[^0]"};

You can have only filter, or a hundred, it's pretty flexible, and it resembles dial peer pattern matching quite nicely.

If you had a script with just this one line in it, you could call it via a Subflow, passing the phoneNumber to test, and have your self a handy centralized phone number validation tool which any script could use.

joshuamarsh Thu, 12/17/2009 - 10:15

Great info. Thank you both.

Anthony, your answer led to me learning several things that I knew nothing about.  Thanks much, that just opened up my scripting world a bit.  That's very nice to be able to run it through filters just like dial peers.  That's exactly what I was looking for.

Best,

Joshua

joshuamarsh Mon, 12/21/2009 - 09:06

Put Anthony's code in a "Do" step, which is available under the General steps.

Per Anthony's suggestion, I have a script w/ nothing in it but his code in a Do step.  I pass my phone number and boolean into the script and it returns a true or false back which tells me if it is a valid NANP number or not.

J

joshuamarsh Mon, 12/21/2009 - 11:11

Mike,

There is a "Do" step in the General steps section.  Just drop the snippet into the Do step, create your variables, setup your filters, and it works like a champ.

Joshua

Actions

This Discussion