Java
  Home arrow Java arrow Page 7 - Introduction to the Java.util.regex Object...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Introduction to the Java.util.regex Object Model
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2005-08-18

    Table of Contents:
  • Introduction to the Java.util.regex Object Model
  • public static Pattern compile(String regex, int flags) Throws a PatternSyntaxException
  • public String[] split(CharSequence input)
  • The Matcher Object
  • public int start(int group)
  • public int end(int group)
  • public String group(int group)
  • public boolean find()
  • public Matcher appendReplacement (StringBuffer sb, String replacement)
  • Special Notes
  • New String Rejex-Friendly Methods
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introduction to the Java.util.regex Object Model - public String group(int group)


    (Page 7 of 12 )

    This method is a more powerful counterpart to the group() method. It allows you to extract parts of a candidate String that match a subgroup within your pattern. The use of the group(int) method is demonstrated shortly in Listing 2-14.

    In the following example, the regex pattern is again B(ond), which means you have a subgroup within the pattern. The portion the candidate parsed when find() is called for the first time is shown here:

     

    Thus, when you call the group(0) method, you’re implicitly calling it only for the region that has already been parsed, which is boxed in the preceding image. As far as the Matcher is currently concerned, this boxed region is the only one we can discuss.

    Calling group(0) returns Bond because that’s the first group that matches your criteria in the region of the candidate String currently under inspection. Again, that area is shown in the box in the preceding image. The actual matching group is shown in the following image:

     

    Similarly, when you call group(1), you’re calling it only for the region that has already been parsed—again, the boxed area. This time, you’re asking for the second grouping in the parsed region. group(1) is circled in the following image:

     

    Next, you call matcher.find() again, which results in a new region of the candidate String coming under inspection, as shown here:

     

    Calling the group(0) method implicitly calls it only for the new region that has already been parsed, which is boxed in the preceding image. The group(0) method returns the String Bond. group(0) is circled in the following image:

     

    Calling group(1) only considers the new region that been parsed—again, the boxed region. Within that region, group(1) refers to ond. group(1) is circled in the following image:

     

    Listing 2-14 presents an example using the group(int) method, and Output 2-8 shows the output of this example.

    Listing 2-14. Matcher.group(int) Method Example

    import java.util.regex.*;
    /**
     
    * Demonstrates the usage of the
     * Matcher.group(int) method
     */
    public class MatcherGroupParamExample{
      public static void main(String args[]){
         
    test();
      }
      public static void test(){
       
    //create a Pattern
         Pattern p = Pattern.compile("B(ond)");
       
    //create a Matcher and use the Matcher.group(int) method
        String candidateString = "My name is Bond. James Bond.";
        //create a helpful index for the sake of output
        Matcher matcher = p.matcher(candidateString);
        //Find group number 0 of the first find
        
    matcher.find();
         String group_0 = matcher.group(0);
         String group_1 = matcher.group(1);
         System.out.println("Group 0 " + group_0); 
         System.out.println("Group 1 " + group_1);
         System.out.println(candidateString);
       
    //Find group number 1 of the second find
         matcher.find();
         group_0 = matcher.group(0);
         group_1 = matcher.group(1);
         System.out.println("Group 0 " + group_0);
         System.out.println("Group 1 " + group_1);
         System.out.println(candidateString);
     
    }
    }

    Output 2-8. Output of the Matcher.Group(int) Example

    ------------------------------------------------------------------My name is Bond. James Bond.
    Group 0 Bond
    Group 1 ond
    My name is Bond. James Bond.
    Group 0 Bond
    Group 1 ond

    If you execute another find() method

    matcher.find();

    and then execute group(0)

    String tmp = matcher.group(0); //throws IllegalStateException

    the group(0) method will throw an IllegalStateException because the find method call wasn’t successful. Similarly, it will throw an IllegalStateException if find hadn’t been called at all. If you try to refer to a group number that doesn’t exist, it will throw an IndexOutOfBoundsException.

    public int groupCount()

    This method simply returns the number of groups that the Pattern defined. In Listing 2-15, the groupCount method displays the number of possible groups a given pattern might have.

    Listing 2-15. MatcherGroupCountExample Example

    import java.util.regex.*;
    /**
     
    * Demonstrates the usage of the
     
    * Matcher.groupCount() method
     */
    public class MatcherGroupCountExample{
      public static void main(String args[]){
          test();
      }
      public static void test(){
        //create a Pattern Pattern
        p = Pattern.compile("B(ond)");
       
    //create a Matcher and use the Matcher.group() method
        String candidateString = "My name is Bond. James Bond.";
        Matcher matcher = p.matcher(candidateString);
       
    //extract the possible number of groups.
       
    //It's important to be aware that this
       
    //represents only the number of groups that
       
    //are possible: not the actual number of groups
       
    //found in the candidate string
       
    int numberOfGroups = matcher.groupCount();
       
    System.out.println("numberOfGroups ="+numberOfGroups);
     
    }
    }

    There’s a very important, and somewhat counterintuitive, subtlety to notice about this method. It returns the number of possible groups based on the original Pattern, without even considering the candidate String. Thus, it’s not really information about the Matcher object; rather, it’s information about the Pattern that helped spawn it. This can be tricky, because the fact that this method lives on the Matcher object could be interpreted to mean that it’s providing feedback about the state of the Matcher. It just isn’t. It’s telling you how many matches are theoretically possible for the given Pattern.

    public boolean matches()

    This method is designed to help you match a candidate String against the Matcher’s Pattern. If it returns true if—and only if—the candidate String under consideration matches the pattern exactly.

    Listing 2-16 demonstrates how you might use this method. Three strings, j2se, J2SE, and J2SE (notice the space after the E), are compared to the Pattern J2SE.

    Listing 2-16. Matcher.matches Example

        import java.util.regex.*;
    /**
     
    * Demonstrates the usage of the
     
    * Matcher.matches method
     */
    public class MatcherMatchesExample{
      public static void main(String args[]){
          test();
      }
      public static void test(){
        
    //create a Pattern
          Pattern p = Pattern.compile("J2SE");
       
    //create the candidate Strings
        String candidateString_1 = "j2se";
        String candidateString_2 = "J2SE ";
        String candidateString_3 = "J2SE";
       
    //Attempt to match the candidate Strings.
        Matcher matcher_1 = p.matcher(candidateString_1); 
        Matcher matcher_2 = p.matcher(candidateString_2);
        Matcher matcher_3 = p.matcher(candidateString_3);
       
    //display the output for first candidate
        String msg = ":" + candidateString_1 + ": matches?: ";
        System.out.println( msg + matcher_1.matches());
       
    //display the output for second candidate
        msg = ":" + candidateString_2 + ": matches?: ";
        System.out.println(msg + matcher_2.matches());
       
    //display the output for third candidate
        msg = ":" + candidateString_3 + ": matches?: ";
        System.out.println(msg + matcher_3.matches());
     
    }
    }

    Only one of the three candidates successfully matches here. j2se is rejected because it is the wrong case. J2SE is again rejected because it contains a space character after the E, which means that it isn’t a perfect match. The only perfect match is J2SE.

    More Java Articles
    More By Apress Publishing


     

    Buy this book now. This article is excerpted from chapter three of Java Regular Expressions Taming the Java.util.regex Engine, written by Mehran Habibi (Apress, 2004; ISBN: 1590591070). Check it out at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT