Java
  Home arrow Java arrow Page 4 - Advanced Regex
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

Advanced Regex
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2005-07-07

    Table of Contents:
  • Advanced Regex
  • Noncapturing Subgroups
  • Greedy Qualifiers
  • Reluctant Qualifiers
  • Understanding Lookarounds
  • Zen and the Art of Efficient Expressions
  • 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


    Advanced Regex - Reluctant Qualifiers


    (Page 4 of 7 )

    At the other end of the spectrum from greedy qualifiers are reluctant qualifiers, which try to match as little as possible. Reluctant qualifiers are formed by appending? to an existing greedy qualifier. Thus, X+ becomes X+?, X(n,m} becomes X{n,m}?, and so on. Given the pattern \d+? against the candidate string 1234, for example, the resultant match is 1, as Listing 3-7 demonstrates.

    Listing 3-7. Reluctant Qualifier Example

    import java.util.regex.*;
    public class ReluctantExample{
     
    public static void main(String args[]){
        //define the pattern
        String regex = "(\\d+?)";
        //compile the pattern
        Pattern pattern = Pattern.compile(regex);
       
    //define the candidate string
        String candidate = "1234";
       
    //extract a matcher for the candidate string
        Matcher matcher = pattern.matcher(candidate);
       
    while (matcher.find()){
          //matches once for each digit
          //if this were not an example of a
          //reluctant qualifier, it would match
          //exactly once, and that match would
          //include every digit in the candidate
          //string "1234".
          System.out.println(matcher.group());
       
    }
       
    System.out.println("Done");
      }
    }

    Every time find() is run, it matches as little as possible, because it’s reluctant to match. The Pattern matches exactly four times: once for each digit. If you weren’t using a reluctant qualifier in the Pattern, there would have been a single match for the entire candidate string, namely 1234, because the Pattern would have been greedy and matched as much as possible.

    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 1 hosted by Hostway
    Stay green...Green IT