Class Regsub


  • public class Regsub
    extends java.lang.Object
    The Regsub class provides an iterator-like object to extract the matched and unmatched portions of a string with respect to a given regular expression.

    After each match is found, the portions of the string already checked are not searched again -- searching for the next match will begin at the character just after where the last match ended.

    Here is an example of using Regsub to replace all "%XX" sequences in a string with the ASCII character represented by the hex digits "XX":

     public static void
     main(String[] args)
         throws Exception
     {
         Regexp re = new Regexp("%[a-fA-F0-9][a-fA-F0-9]");
         Regsub rs = new Regsub(re, args[0]);
    
         StringBuffer sb = new StringBuffer();
    
         while (rs.nextMatch()) {
             sb.append(rs.skipped());
    
             String match = rs.matched();
    
             int hi = Character.digit(match.charAt(1), 16);
             int lo = Character.digit(match.charAt(2), 16);
             sb.append((char) ((hi << 4) | lo));
         }
         sb.append(rs.rest());
    
         System.out.println(sb);
     }
     
    Version:
    2.3
    Author:
    Colin Stevens (colin.stevens@sun.com)
    See Also:
    Regexp
    • Constructor Summary

      Constructors 
      Constructor Description
      Regsub​(Regexp r, java.lang.String str)
      Construct a new Regsub that can be used to step through the given string, finding each substring that matches the given regular expression.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Regexp getRegexp()
      Return the regexp used by this regsub.
      java.lang.String matched()
      Returns a substring consisting of the characters that matched the entire regular expression during the last call to nextMatch.
      boolean nextMatch()
      Searches for the next substring that matches the regular expression.
      java.lang.String rest()
      Returns a substring consisting of all the characters that come after the last match.
      java.lang.String skipped()
      Returns a substring consisting of all the characters skipped between the end of the last match (or the start of the original search string) and the start of this match.
      java.lang.String submatch​(int i)
      Returns a substring consisting of the characters that matched the given parenthesized subexpression during the last call to nextMatch.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Regsub

        public Regsub​(Regexp r,
                      java.lang.String str)
        Construct a new Regsub that can be used to step through the given string, finding each substring that matches the given regular expression.

        Regexp contains two substitution methods, sub and subAll, that can be used instead of Regsub if just simple substitutions are being done.

        Parameters:
        r - The compiled regular expression.
        str - The string to search.
        See Also:
        Regexp.sub(java.lang.String, java.lang.String), Regexp.subAll(java.lang.String, java.lang.String)
    • Method Detail

      • nextMatch

        public boolean nextMatch()
        Searches for the next substring that matches the regular expression. After calling this method, the caller would call methods like skipped, matched, etc. to query attributes of the matched region.

        Calling this function again will search for the next match, beginning at the character just after where the last match ended.

        Returns:
        true if a match was found, false if there are no more matches.
      • skipped

        public java.lang.String skipped()
        Returns a substring consisting of all the characters skipped between the end of the last match (or the start of the original search string) and the start of this match.

        This method can be used extract all the portions of string that didn't match the regular expression.

        Returns:
        The characters that didn't match.
      • matched

        public java.lang.String matched()
        Returns a substring consisting of the characters that matched the entire regular expression during the last call to nextMatch.
        Returns:
        The characters that did match.
        See Also:
        submatch(int)
      • submatch

        public java.lang.String submatch​(int i)
        Returns a substring consisting of the characters that matched the given parenthesized subexpression during the last call to nextMatch.
        Parameters:
        i - The index of the parenthesized subexpression.
        Returns:
        The characters that matched the subexpression, or null if the given subexpression did not exist or did not match.
      • rest

        public java.lang.String rest()
        Returns a substring consisting of all the characters that come after the last match. As the matches progress, the rest gets shorter. When nextMatch returns false, then this method will return the rest of the string that can't be matched.
        Returns:
        The rest of the characters after the last match.
      • getRegexp

        public Regexp getRegexp()
        Return the regexp used by this regsub.