The Power of Regular Expressions
Regular Expressions (RegEx for short) are a very
powerful tool that can save a LOT of lines of code especially when validating
input data...
Learning RegEx
can be a little daunting, but with the help of a RegEx Explorer/Coach tool, life
can be very easy.For Mac, I recommend
RegexPlor
(downloadable at MacUpdate ) and
for Windows, try the really excellent Regex Coach . Simply build
your expression and test it in one of these apps. Then copy/paste the regex into
your code and escape all the slashes by replacing every single slash with 2
slashes.A common use of RegEx in
WebObjects java is validating user input using java's matches
method of the String
class. Alternatively use the Pattern
class for better efficiency in repetitive use of the
code.This online tutorial
site has dedicated java
information too with the most important thing to remember is to
escape the RegEx strings that you use as arguments in
java.Since java uses a single slash as
an escape character, then all Regex escapes must be double slashes which is an
escaped slash!Here is a simple
command line java class for checking email addresses. You can compile it on the
command line and run it with an email address
argument.public class EmailRegEx
{ public static void main (String args[])
{ String aString =
args[0]; String regex = new
String( "^[a-zA-Z0-9\\._%-]+@[a-zA-Z0-9\\._%-]+\\.[a-zA-Z]{2,4}$"
); System.out.println("Regular
Expression: " + regex); if
(aString.matches(regex) == true)
{ System.out.println("GOOD: " +
aString); } else
{ System.out.println("BAD : " +
aString); }
}}Another
example, here is a validation method for a zip code
field:
public
static
String validateZip(Object aValue)
throws
NSValidation.ValidationException
{ String errorMessage =
"Zip Code must be entered as 99999 or
99999-9999."; //
Regex string:
^\d{5}$|^\d{5}\-\d{4}$ String zipRegex
=
"^\\d{5}$|^\\d{5}\\-\\d{4}$"; Object
theValue; if
(aValue
instanceof
Number) { theValue =
(Object)((Number)aValue).toString(); }
else
{ theValue =
aValue; } if
(theValue
instanceof
String) {
if(
((String)theValue).matches(zipRegex) ==
true )
{ //
we have a valid
zip return
(String)theValue; }
else
{ throw
new
NSValidation.ValidationException(errorMessage);
} }
else
{
throw
new
NSValidation.ValidationException(errorMessage); }
}Here is a validation for US
style phone numbers...
/** Checks that an object is a valid
phone number @param
Object aValue @param
field name for constructing error message. Can be
null. @return the
valid phone number digits only */
public
static
String validatePhone(Object aValue, String fieldName)
throws
NSValidation.ValidationException
{ String
errorMessage; if
(fieldName ==
null)
{ errorMessage =
"Must be entered as 999-999-9999 or
(999) 999-9999 or
9999999999."; }
else
{ errorMessage = fieldName +
" must be entered as 999-999-9999 or
(999) 999-9999 or
9999999999."; }
String phoneRegex =
"^\\(?\\d{3}(\\)?|-?)\\s*\\d{3}-?\\d{4}$"; Object
theValue; if
(aValue
instanceof
Number) { theValue =
(Object)((Number)aValue).toString(); }
else
{ theValue =
aValue; } if
(theValue
instanceof
String) {
if(
((String)theValue).matches(phoneRegex) ==
true )
{ //
we have a valid phone
number //
strip everything but the
digits return
((String)theValue).replaceAll("\\D","");
} else
{ throw
new
NSValidation.ValidationException(errorMessage);
} }
else
{
throw
new
NSValidation.ValidationException(errorMessage); }
}Some other useful
Regex...Checking a login name of
between 6 and 32 characters beginning with a letter and allowed to have numbers,
letters and these chars @ _ - .
$String loginNameRegex =
"^[a-zA-Z][@_\\-\\.\\$\\w]{5,31}$";Credit
Card Security Code. Either 3 or 4
charactersString regex =
"^\\d{3}$|^\\d{4}$";
Posted: Tuesday - January 18, 2005 at 05:05 PM