cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2079
Views
15
Helpful
6
Replies

Regular Expression in a Script

joshuamarsh
Level 1
Level 1

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

1 Accepted Solution

Accepted Solutions

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.

View solution in original post

6 Replies 6

Jonathan Schulenberg
Hall of Fame
Hall of Fame

If you are just looking to validate that they entered a seven-character string, you can use the .length() Java method.

Example:  IF myVariable.length() == 7

If you want to do actual expressions, you need to look at the .matches() Java method. Here's some documentation to get you started:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

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.

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

Anthony how would I apply your code? in a set command?

Thanks,

Mike

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

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

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: