Cisco Support Community
cancel
Showing results for 
Search instead for 
Did you mean: 
New Member

Regular Expression in a Script

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

Re: Regular Expression in a Script

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.

Anthony Holloway

Please use the star ratings to help drive great content to the top of searches.
6 REPLIES
VIP Super Bronze

Re: Regular Expression in a Script

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

Re: Regular Expression in a Script

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.

Anthony Holloway

Please use the star ratings to help drive great content to the top of searches.
New Member

Re: Regular Expression in a Script

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

New Member

Re: Regular Expression in a Script

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

Thanks,

Mike

New Member

Re: Regular Expression in a Script

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

New Member

Re: Regular Expression in a Script

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

827
Views
15
Helpful
6
Replies
CreatePlease to create content