Here you can find the source of quote(String s)
Parameter | Description |
---|---|
s | The string to match. |
public static String quote(String s)
//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(); } }