Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.apache.oro.text.regex
Class Util  view Util download Util.java

java.lang.Object
  extended byorg.apache.oro.text.regex.Util

public final class Util
extends java.lang.Object

The Util class is a holder for useful static utility methods that can be generically applied to Pattern and PatternMatcher instances. This class cannot and is not meant to be instantiated. The Util class currently contains versions of the split() and substitute() methods inspired by Perl's split function and s operation respectively, although they are implemented in such a way as not to rely on the Perl5 implementations of the OROMatcher packages regular expression interfaces. They may operate on any interface implementations conforming to the OROMatcher API specification for the PatternMatcher, Pattern, and MatchResult interfaces. Future versions of the class may include additional utility methods.

A grep method is not included for two reasons:

  1. The details of reading a line at a time from an input stream differ in JDK 1.0.2 and JDK 1.1, making it difficult to retain compatibility across both Java releases.
  2. Grep style processing is trivial for the programmer to implement in a while loop. Rarely does anyone want to retrieve all occurences of a pattern and then process them. More often a programmer will retrieve pattern matches and process them as they are retrieved, which is more efficient than storing them all in a Vector and then accessing them.

Since:
1.0
Version:
@version@

Field Summary
static int SPLIT_ALL
          A constant passed to the split() 55 methods indicating that all occurrences of a pattern should be used to split a string.
static int SUBSTITUTE_ALL
          A constant passed to the substitute() 55 methods indicating that all occurrences of a pattern should be substituted.
 
Constructor Summary
private Util()
          The default destructor for the Util class.
 
Method Summary
static void split(java.util.Collection results, PatternMatcher matcher, Pattern pattern, java.lang.String input)
          Splits up a String instance and stores results as a Collection of all its substrings using a regular expression as the delimiter.
static void split(java.util.Collection results, PatternMatcher matcher, Pattern pattern, java.lang.String input, int limit)
          Splits up a String instance and stores results as a List of substrings numbering no more than a specified limit.
static java.util.Vector split(PatternMatcher matcher, Pattern pattern, java.lang.String input)
          Deprecated. Use split(Collection, PatternMatcher, Pattern, String) 55 instead.
static java.util.Vector split(PatternMatcher matcher, Pattern pattern, java.lang.String input, int limit)
          Deprecated. Use split(Collection, PatternMatcher, Pattern, String, int) 55 instead.
static java.lang.String substitute(PatternMatcher matcher, Pattern pattern, Substitution sub, java.lang.String input)
          Searches a string for a pattern and substitutes only the first occurence of the pattern.
static java.lang.String substitute(PatternMatcher matcher, Pattern pattern, Substitution sub, java.lang.String input, int numSubs)
          Searches a string for a pattern and replaces the first occurrences of the pattern with a Substitution up to the number of substitutions specified by the numSubs parameter.
static int substitute(java.lang.StringBuffer result, PatternMatcher matcher, Pattern pattern, Substitution sub, PatternMatcherInput input, int numSubs)
          Searches a string for a pattern and replaces the first occurrences of the pattern with a Substitution up to the number of substitutions specified by the numSubs parameter.
static int substitute(java.lang.StringBuffer result, PatternMatcher matcher, Pattern pattern, Substitution sub, java.lang.String input, int numSubs)
          Searches a string for a pattern and replaces the first occurrences of the pattern with a Substitution up to the number of substitutions specified by the numSubs parameter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SUBSTITUTE_ALL

public static final int SUBSTITUTE_ALL
A constant passed to the substitute() 55 methods indicating that all occurrences of a pattern should be substituted.

See Also:
Constant Field Values

SPLIT_ALL

public static final int SPLIT_ALL
A constant passed to the split() 55 methods indicating that all occurrences of a pattern should be used to split a string.

See Also:
Constant Field Values
Constructor Detail

Util

private Util()
The default destructor for the Util class. It is made private to prevent the instantiation of the class.

Method Detail

split

public static void split(java.util.Collection results,
                         PatternMatcher matcher,
                         Pattern pattern,
                         java.lang.String input,
                         int limit)
Splits up a String instance and stores results as a List of substrings numbering no more than a specified limit. The string is split with a regular expression as the delimiter. The limit parameter essentially says to split the string only on at most the first limit - 1 number of pattern occurences.

This method is inspired by the Perl split() function and behaves identically to it when used in conjunction with the Perl5Matcher and Perl5Pattern classes except for the following difference:

    In Perl, if the split expression contains parentheses, the split() method creates additional list elements from each of the matching subgroups in the pattern. In other words:

      split(list, "/([,-])/", "8-12,15,18", Util.SPLIT_ALL)

    produces the list containing:

      { "8", "-", "12", ",", "15", ",", "18" }

    The OROMatcher split method does not follow this behavior. The following list would be produced by OROMatcher:

      { "8", "12", "15", "18" }

    To obtain the Perl behavior, use org.apache.oro.text.perl.Perl5Util#split.

Since:
2.0

split

public static void split(java.util.Collection results,
                         PatternMatcher matcher,
                         Pattern pattern,
                         java.lang.String input)
Splits up a String instance and stores results as a Collection of all its substrings using a regular expression as the delimiter. This method is inspired by the Perl split() function and behaves identically to it when used in conjunction with the Perl5Matcher and Perl5Pattern classes except for the following difference:

    In Perl, if the split expression contains parentheses, the split() method creates additional list elements from each of the matching subgroups in the pattern. In other words:

      split(list, "/([,-])/", "8-12,15,18")

    produces the list containing:

      { "8", "-", "12", ",", "15", ",", "18" }

    The OROMatcher split method does not follow this behavior. The following list would be produced by OROMatcher:

      { "8", "12", "15", "18" }

    To obtain the Perl behavior, use org.apache.oro.text.perl.Perl5Util#split.

This method is identical to calling:

 split(matcher, pattern, input, Util.SPLIT_ALL);
 

Since:
2.0

split

public static java.util.Vector split(PatternMatcher matcher,
                                     Pattern pattern,
                                     java.lang.String input,
                                     int limit)
Deprecated. Use split(Collection, PatternMatcher, Pattern, String, int) 55 instead.

Splits up a String instance into strings contained in a Vector of size not greater than a specified limit. The string is split with a regular expression as the delimiter. The limit parameter essentially says to split the string only on at most the first limit - 1 number of pattern occurences.

This method is inspired by the Perl split() function and behaves identically to it when used in conjunction with the Perl5Matcher and Perl5Pattern classes except for the following difference:

    In Perl, if the split expression contains parentheses, the split() method creates additional list elements from each of the matching subgroups in the pattern. In other words:

      split("/([,-])/", "8-12,15,18")

    produces the Vector containing:

      { "8", "-", "12", ",", "15", ",", "18" }

    The OROMatcher split method does not follow this behavior. The following Vector would be produced by OROMatcher:

      { "8", "12", "15", "18" }

    To obtain the Perl behavior, use org.apache.oro.text.perl.Perl5Util#split.

Since:
1.0

split

public static java.util.Vector split(PatternMatcher matcher,
                                     Pattern pattern,
                                     java.lang.String input)
Deprecated. Use split(Collection, PatternMatcher, Pattern, String) 55 instead.

Splits up a String instance into a Vector of all its substrings using a regular expression as the delimiter. This method is inspired by the Perl split() function and behaves identically to it when used in conjunction with the Perl5Matcher and Perl5Pattern classes except for the following difference:

    In Perl, if the split expression contains parentheses, the split() method creates additional list elements from each of the matching subgroups in the pattern. In other words:

      split("/([,-])/", "8-12,15,18")

    produces the Vector containing:

      { "8", "-", "12", ",", "15", ",", "18" }

    The OROMatcher split method does not follow this behavior. The following Vector would be produced by OROMatcher:

      { "8", "12", "15", "18" }

    To obtain the Perl behavior, use org.apache.oro.text.perl.Perl5Util#split.

This method is identical to calling:

 split(matcher, pattern, input, Util.SPLIT_ALL);
 

Since:
1.0

substitute

public static java.lang.String substitute(PatternMatcher matcher,
                                          Pattern pattern,
                                          Substitution sub,
                                          java.lang.String input,
                                          int numSubs)
Searches a string for a pattern and replaces the first occurrences of the pattern with a Substitution up to the number of substitutions specified by the numSubs parameter. A numSubs value of SUBSTITUTE_ALL will cause all occurrences of the pattern to be replaced.

Since:
1.0

substitute

public static java.lang.String substitute(PatternMatcher matcher,
                                          Pattern pattern,
                                          Substitution sub,
                                          java.lang.String input)
Searches a string for a pattern and substitutes only the first occurence of the pattern.

This method is identical to calling:

 substitute(matcher, pattern, sub, input, 1);
 

Since:
1.0

substitute

public static int substitute(java.lang.StringBuffer result,
                             PatternMatcher matcher,
                             Pattern pattern,
                             Substitution sub,
                             java.lang.String input,
                             int numSubs)
Searches a string for a pattern and replaces the first occurrences of the pattern with a Substitution up to the number of substitutions specified by the numSubs parameter. A numSubs value of SUBSTITUTE_ALL will cause all occurrences of the pattern to be replaced. The number of substitutions made is returned.

Since:
2.0.6

substitute

public static int substitute(java.lang.StringBuffer result,
                             PatternMatcher matcher,
                             Pattern pattern,
                             Substitution sub,
                             PatternMatcherInput input,
                             int numSubs)
Searches a string for a pattern and replaces the first occurrences of the pattern with a Substitution up to the number of substitutions specified by the numSubs parameter. A numSubs value of SUBSTITUTE_ALL will cause all occurrences of the pattern to be replaced. The number of substitutions made is returned.

Since:
2.0.3