You are viewing a plain text version of this content. The canonical link for it is here.
Posted to regexp-dev@jakarta.apache.org by vg...@apache.org on 2003/06/02 04:18:41 UTC

cvs commit: jakarta-regexp/xdocs changes.xml

vgritsenko    2003/06/01 19:18:41

  Modified:    docs     changes.html
               src/java/org/apache/regexp RE.java
               xdocs    changes.xml
  Log:
  subst() can now process backreferences when flag REPLACE_BACKREFERENCES is set.
  Submitted by: Tobias Schaefer <to...@gmx.de>
  Reviewed by: Jonathan Locke
  
  Revision  Changes    Path
  1.9       +3 -0      jakarta-regexp/docs/changes.html
  
  Index: changes.html
  ===================================================================
  RCS file: /home/cvs/jakarta-regexp/docs/changes.html,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- changes.html	2 May 2003 18:04:06 -0000	1.8
  +++ changes.html	2 Jun 2003 02:18:41 -0000	1.9
  @@ -82,6 +82,9 @@
   </p>
                                                   <h3>Version 1.3-dev</h3>
                                                   <ul>
  +<li>New Feature: subst() can now process backreferences when flag
  +    REPLACE_BACKREFERENCES is set. See API docs for details.
  +    Patch provided by Tobias Schaefer. (VG)</li>
   <li>Applied patches for Bug 
       <a href="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16592">16592</a>:
       Syntax error: Too many bracketed closures (limit is 10) (VG)</li>
  
  
  
  1.13      +60 -6     jakarta-regexp/src/java/org/apache/regexp/RE.java
  
  Index: RE.java
  ===================================================================
  RCS file: /home/cvs/jakarta-regexp/src/java/org/apache/regexp/RE.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- RE.java	2 May 2003 01:03:47 -0000	1.12
  +++ RE.java	2 Jun 2003 02:18:41 -0000	1.13
  @@ -364,6 +364,7 @@
    * @see RECompiler
    *
    * @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
  + * @author <a href="mailto:ts@sch-fer.de">Tobias Sch&auml;fer</a>
    * @version $Id$
    */
   public class RE implements Serializable
  @@ -1651,13 +1652,18 @@
        * Flag bit that indicates that subst should replace all occurrences of this
        * regular expression.
        */
  -    public static final int REPLACE_ALL          = 0x0000;
  +    public static final int REPLACE_ALL            = 0x0000;
   
       /**
        * Flag bit that indicates that subst should only replace the first occurrence
        * of this regular expression.
        */
  -    public static final int REPLACE_FIRSTONLY    = 0x0001;
  +    public static final int REPLACE_FIRSTONLY      = 0x0001;
  +
  +    /**
  +     * Flag bit that indicates that subst should replace backreferences
  +     */
  +    public static final int REPLACE_BACKREFERENCES = 0x0002;
   
       /**
        * Substitutes a string for this regular expression in another string.
  @@ -1665,6 +1671,7 @@
        * Given a regular expression of "a*b", a String to substituteIn of
        * "aaaabfooaaabgarplyaaabwackyb" and the substitution String "-", the
        * resulting String returned by subst would be "-foo-garply-wacky-".
  +     *
        * @param substituteIn String to substitute within
        * @param substitution String to substitute for all matches of this regular expression.
        * @return The string substituteIn with zero or more occurrences of the current
  @@ -1683,12 +1690,23 @@
        * Given a regular expression of "a*b", a String to substituteIn of
        * "aaaabfooaaabgarplyaaabwackyb" and the substitution String "-", the
        * resulting String returned by subst would be "-foo-garply-wacky-".
  +     * <p>
  +     * It is also possible to reference the contents of a parenthesized expression
  +     * with $0, $1, ... $9. A regular expression of "http://[\\.\\w\\-\\?/~_@&=%]+",
  +     * a String to substituteIn of "visit us: http://www.apache.org!" and the
  +     * substitution String "&lt;a href=\"$0\"&gt;$0&lt;/a&gt;", the resulting String
  +     * returned by subst would be 
  +     * "visit us: &lt;a href=\"http://www.apache.org\"&gt;http://www.apache.org&lt;/a&gt;!".
  +     * <p>
  +     * <i>Note:</i> $0 represents the whole match.
  +     *
        * @param substituteIn String to substitute within
        * @param substitution String to substitute for matches of this regular expression
        * @param flags One or more bitwise flags from REPLACE_*.  If the REPLACE_FIRSTONLY
        * flag bit is set, only the first occurrence of this regular expression is replaced.
        * If the bit is not set (REPLACE_ALL), all occurrences of this pattern will be
  -     * replaced.
  +     * replaced. If the flag REPLACE_BACKREFERENCES is set, all backreferences will
  +     * be processed.
        * @return The string substituteIn with zero or more occurrences of the current
        * regular expression replaced with the substitution String (if this regular
        * expression object doesn't match at any position, the original String is returned
  @@ -1709,8 +1727,44 @@
               // Append string before match
               ret.append(substituteIn.substring(pos, getParenStart(0)));
   
  -            // Append substitution
  -            ret.append(substitution);
  +            if ((flags & REPLACE_BACKREFERENCES) != 0)
  +            {
  +                // Process backreferences
  +                int lCurrentPosition = 0;
  +                int lLastPosition = 0;
  +                int lLength = substitution.length();
  +
  +                while ((lCurrentPosition = substitution.indexOf("$", lCurrentPosition)) >= 0)
  +                {
  +                    if ((lCurrentPosition == 0 || substitution.charAt(lCurrentPosition - 1) != '\\')
  +                        && lCurrentPosition+1 < lLength)
  +                    {
  +                        char c = substitution.charAt(lCurrentPosition + 1);
  +                        if (c >= '0' && c <= '9')
  +                        {
  +                            // Append everything between the last and the current $ sign
  +                            ret.append(substitution.substring(lLastPosition+2, lCurrentPosition));
  +
  +                            // Append the parenthesized expression
  +                            // Note: if a parenthesized expression of the requested
  +                            // index is not available "null" is added to the string
  +                            ret.append(getParen(c - '0'));
  +                            lLastPosition = lCurrentPosition;
  +                        }
  +                    }
  +
  +                    // Move forward, skipping past match
  +                    lCurrentPosition++;
  +                }
  +
  +                // Append everything after the last $ sign
  +                ret.append(substitution.substring(lLastPosition+2,lLength));
  +            }
  +            else
  +            {
  +                // Append substitution without processing backreferences
  +                ret.append(substitution);
  +            }
   
               // Move forward, skipping past match
               int newpos = getParenEnd(0);
  
  
  
  1.11      +4 -2      jakarta-regexp/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-regexp/xdocs/changes.xml,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- changes.xml	2 May 2003 18:04:06 -0000	1.10
  +++ changes.xml	2 Jun 2003 02:18:41 -0000	1.11
  @@ -1,6 +1,6 @@
   <?xml version="1.0"?>
  -<document>
   
  +<document>
     <properties>
       <author email="jon@latchkey.com">Jon S. Stevens</author>
       <author email="vgritsenko@apache.org">Vadim Gritsenko</author>
  @@ -8,7 +8,6 @@
     </properties>
   
   <body>
  -
     <section name="Regexp Changes">
   
   <p>
  @@ -18,6 +17,9 @@
   
   <h3>Version 1.3-dev</h3>
   <ul>
  +<li>New Feature: subst() can now process backreferences when flag
  +    REPLACE_BACKREFERENCES is set. See API docs for details.
  +    Patch provided by Tobias Schaefer. (VG)</li>
   <li>Applied patches for Bug 
       <a href="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16592">16592</a>:
       Syntax error: Too many bracketed closures (limit is 10) (VG)</li>
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: regexp-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: regexp-dev-help@jakarta.apache.org