commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henri Yandell (JIRA)" <j...@apache.org>
Subject [jira] Created: (LANG-373) Qutoe regexp
Date Tue, 13 Nov 2007 13:48:50 GMT
Qutoe regexp
------------

                 Key: LANG-373
                 URL: https://issues.apache.org/jira/browse/LANG-373
             Project: Commons Lang
          Issue Type: New Feature
            Reporter: Henri Yandell
             Fix For: LangTwo 1.0


Once Lang is 1.4 dependent; add this from String Taglib's StringW (suitably changed for Java's
syntax):

    /**
     * Quote a string so that it may be used in a regular expression
     * without any parts of the string being considered as a
     * part of the regular expression's control characters.
     */
    static public String quoteRegularExpression(String str) {
        // replace ? + * / . ^ $ as long as they're not in character
        // class. so must be done by hand
        char[] chrs = str.toCharArray();
        int sz = chrs.length; 
        StringBuffer buffer = new StringBuffer(2*sz);
        for(int i=0; i<sz; i++) {
            switch(chrs[i]) {
              case '[' :
              case ']' :
              case '?' :
              case '+' :
              case '*' :
              case '/' :
              case '.' :
              case '^' :
              case '$' :
                buffer.append("\\");
              default : 
                buffer.append(chrs[i]);
            }
        }
        return buffer.toString();
    }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message