Java String Quote quote(String s)

Here you can find the source of quote(String s)

Description

Takes a string and returns the regex that will match that string exactly.

License

Apache License

Parameter

Parameter Description
s The string to match.

Return

The regex that will match the string exactly.

Declaration

public static String quote(String s) 

Method Source Code

//package com.java2s;
/*/* w w w  .ja va  2s  .c o  m*/
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

public class Main {
    /**
     * The end of a regex literal sequence.
     *
     * @since 1.3
     */
    public static final String REGEX_QUOTE_END = "\\E";
    /**
     * The start of a regex literal sequence.
     *
     * @since 1.3
     */
    public static final String REGEX_QUOTE_START = "\\Q";
    /**
     * Escape the escapes.
     *
     * @since 1.3
     */
    public static final String REGEX_QUOTE_END_ESCAPED = REGEX_QUOTE_END + '\\' + REGEX_QUOTE_END
            + REGEX_QUOTE_START;

    /**
     * Takes a string and returns the regex that will match that string exactly.
     *
     * @param s The string to match.
     * @return The regex that will match the string exactly.
     * @since 1.3
     */
    public static String quote(String s) {
        int i = s.indexOf(REGEX_QUOTE_END);
        if (i == -1) {
            // we're safe as nobody has a crazy \E in the string
            return REGEX_QUOTE_START + s + REGEX_QUOTE_END;
        }

        // damn there's at least one \E in the string
        StringBuffer sb = new StringBuffer(s.length() + 32);
        // each escape-escape takes 10 chars...
        // hope there's less than 4 of them

        sb.append(REGEX_QUOTE_START);
        int pos = 0;
        do {
            // we are safe from pos to i
            sb.append(s.substring(pos, i));
            // now escape-escape
            sb.append(REGEX_QUOTE_END_ESCAPED);
            // move the working start
            pos = i + REGEX_QUOTE_END.length();
            i = s.indexOf(REGEX_QUOTE_END, pos);
        } while (i != -1);

        sb.append(s.substring(pos, s.length()));
        sb.append(REGEX_QUOTE_END);

        return sb.toString();
    }
}

Related

  1. quote(String path)
  2. quote(String s)
  3. quote(String s)
  4. quote(String s)
  5. quote(String s)
  6. quote(String s)
  7. quote(String s)
  8. quote(String s)
  9. quote(String s)