You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Henri Yandell (JIRA)" <ji...@apache.org> on 2007/11/13 14:48:50 UTC

[jira] Created: (LANG-373) Qutoe regexp

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.


[jira] Updated: (LANG-373) Quote regexp

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-373?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-373:
-------------------------------

    Summary: Quote regexp  (was: Qutoe regexp)

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


[jira] Closed: (LANG-373) Quote regexp

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-373?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell closed LANG-373.
------------------------------

    Resolution: Won't Fix

Good point on \Q and \E. Also, there's a Pattern.quote(String) in 1.5+ so this is an unnecessary feature.

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


[jira] Commented: (LANG-373) Quote regexp

Posted by "Sebb (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-373?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12543565 ] 

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

{ and } are also used for repeat counts

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


[jira] Commented: (LANG-373) Quote regexp

Posted by "Sebb (JIRA)" <ji...@apache.org>.
    [ 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.