Regular Expressions


Often you need to match a character sequence against a pattern. Unix and Perl made this operation straightforward with their ubiquitous regular expression libraries. Sun has added a powerful new package called java.util.regex, enabling the use of regular expressions in J2SE 1.4. Sun uses the same regular expressions that are in Perl, so you can use the same expression syntax, which is a relief.

Numerous tutorials and examples on the Web describe the pattern syntax in detail (see "Need to Know More?" at the end of this chapter for two good references). You might consider using the following idea in your project. For the requirement of searching the database, most candidates use the StringTokenizer class to break the search string into its tokens and then process those tokens. This approach works fine for very small search strings. If you want to improve the strength of your search algorithm, however, turn to Java's new pattern-matching capabilities.

The basic task is to use a regular expression to find matches in a string. With a little tweak, you can create an effective search criteria handler. Listing 8.4 gets you started.

Listing 8.4 Using Regular Expressions
 import java.util.regex.*; public final class SearchCriteriaParser {     public static void main(String[] argv)     {         String splitOnThese = "[,'=\"]";         String searchCriteria = "section=\"23\", " +                                 "aisle='first'";         String searchName="", searchValue="";         //compiling sets up the string pattern in the object         //against which to compare the target text.         Pattern p = Pattern.compile(splitOnThese);         //split breaks the text into substrings based on the         //above pattern.         String[] items = p.split(searchCriteria);         for(int i=0;i<items.length;i++)         {             String token = items[i].trim();            if(token.length()>0)            {                if(searchName.length()>0)                {                     searchValue = token;                     processCriteria(searchName,                                     searchValue);                     searchName = "";                } else                {                     searchName = token;                     searchValue = "";                }            }         }     }     public static void processCriteria(String searchName,                                        String searchValue)     {         //Perform search on the database,         //or perhaps save to a list for later searching.         //Presently, print eye candy         System.out.println(searchName + "=" + searchValue);     } } //returns: //section=23 //aisle=first 

Regular expressions are a powerful addition to the J2SE core library. You might consider using them in place of StringTokenizer to parse the search criteria. One advantage of regular expressions over StringTokenizer is that you can use more complex parsing rules. For example, if you wanted to build an SQL query parser, it would be a little easier with the regular expression package. Also, upgrading your application to handle more complicated searches is easier with regular expressions. Although not everyone is a fan of regular expressions, they are a powerful addition to J2SE and worth consideration for your certification project.



JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 187

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net