Regular Expression in a Script

Answered Question
Dec 16th, 2009
User Badges:

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

Correct Answer by Anthony Holloway about 7 years 3 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.
Jonathan Schulenberg Wed, 12/16/2009 - 09:38
User Badges:
  • Super Bronze, 10000 points or more
  • Cisco Designated VIP,

    2017 IP Telephony

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

Correct Answer
Anthony Holloway Wed, 12/16/2009 - 10:24
User Badges:
  • Purple, 4500 points or more

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
User Badges:

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

molivere17982 Sun, 12/20/2009 - 13:04
User Badges:

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


Thanks,

Mike

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

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
User Badges:

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