Home arrow Java arrow Page 4 - Advanced Regex

Advanced Regex

Have you reached the point in your studies of J2SE that you want to learn about some of the more complex regex tools and concepts? This article introduces a variety of concepts, and offers some advice for increasing the efficiency of your regular expressions. It is excerpted from chapter three of Java Regular Expressions Taming the Java.util.regex Engine, written by Mehran Habibi (Apress, 2004; ISBN: 1590591070).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 18
July 07, 2005
  1. · Advanced Regex
  2. · Noncapturing Subgroups
  3. · Greedy Qualifiers
  4. · Reluctant Qualifiers
  5. · Understanding Lookarounds
  6. · Zen and the Art of Efficient Expressions
  7. · Summary

print this article

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".

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.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials