|
Listing 1 includes System.out.println ("Found " + m.group ());. Notice the call to group(). That method is one of three capturing group-oriented Matcher methods: Many languages, including Perl, PHP, Python, JavaScript, and JScript, now support regular expressions for text processing, and some text editors use regular expressions for powerful search-and-replace functionality. What about Java? At the time of this writing, a Java Specification Request that includes a regular expression library for text processing has been approved; you can expect to see it in a future version of the JDK. JDK 1.4 supports regular expressions in the java.util.regex package. Use of this package and supporting classes makes string search and manipulation very easy. It helps reduce the development effort, and at the same time significantly improves the maintenance of code. Since classes in this package are a standard part of core Java, they don't have to be distributed separately, and can be assumed to be present. We will see at the end of article how regular expressions simplify the implementation of email validation. Applications frequently require text processing for features like word searches, email validation, or XML document integrity. This often involves pattern matching. Languages like Perl, sed, or awk improves pattern matching with the use of regular expressions, strings of characters that define patterns used to search for matching text. To pattern match using the Java programming language required the use of the StringTokenizer class with many charAt substring methods to read through the characters or tokens to process the text. This often lead to complex or messy code. In the context of regular expressions, patterns are text representations of sequences of characters. For instance, if you wanted to know if the word car existed within a character sequence, you would use the pattern car because that is how you represent the exact string. For a more complicated pattern, you can use special characters as placeholders. If instead of searching for car, you wanted to search for any string of text that began with the letter c and ended with the letter r, you would use the c*r pattern, where * represents any number of characters before the first r. The c*r pattern would match any string of characters that begins with c and ends with r, as in cougar, cavalier, or chrysler. This free tutorial explains how to use regular expressions to search for and modify patterns in text. The tutorial starts with the basics, and then progresses to intermediate and advanced topics, with lots of examples along the way. This article has shown you how to combine regular expressions and the Java language's internationalization support to validate different kinds of localized data. With this technique, you can support new data types without changing your code at all. Thinking back to the sample application, for example, if you want to add support for Polish postal codes, simply create a messages_pl. properties file. That adds support for a new data type without changing a line of code. (The regular expression for Polish postal codes is [0-9]{2}-?[0-9]{3}, in case you're wondering.) Until recently, Java programmers had to turn to third-party libraries such as IBM's regex for Java. The newest JDK, JDK 1.4, makes regular expressions into first-class citizens. |
w_w___w___.j__a_v___a__2__s_.__c_o_m | Contact Us |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |