commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sebb (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LANG-373) Quote regexp
Date Tue, 13 Nov 2007 17:09:43 GMT

    [ https://issues.apache.org/jira/browse/LANG-373?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12542169
] 

Sebb commented on LANG-373:
---------------------------

The end of the switch statement should be commented to indicate that the drop-thru is intentional:

              case '$' : 
                buffer.append("\\"); 
               // Drop through
              default : 
                buffer.append(chrs[i]); 

But is the method needed? Doesn't regex support the \Q and \E modifiers?

By the way, ( and )  and | are also special to regexes. / is *not* special.

> Quote 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