12-16-2009 09:09 AM - edited 03-14-2019 05:00 AM
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
Solved! Go to Solution.
12-16-2009 10:24 AM
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.
12-16-2009 09:38 AM
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
12-16-2009 10:24 AM
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.
12-17-2009 10:15 AM
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
12-20-2009 01:04 PM
Anthony how would I apply your code? in a set command?
Thanks,
Mike
12-21-2009 09:06 AM
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
12-21-2009 11:11 AM
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
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide