Java String Escape escapeRegexp(final String str)

Here you can find the source of escapeRegexp(final String str)

Description

escape Regexp

License

Open Source License

Declaration

public static String escapeRegexp(final String str) 

Method Source Code


//package com.java2s;

import java.util.Arrays;

public class Main {
    private static final char[] REGEXP_CHARS = new char[] { '\\', '[', ']', '(', ')', '{', '}', '^', '$', '|', '?',
            '*', '+', '-', ':', ',', '.', '&' };

    public static String escapeRegexp(final String str) {
        if (str == null) {
            return null;
        }//w  w  w.jav a  2  s  .c o m

        final StringBuffer buf = new StringBuffer();
        final int len = str.length();
        for (int i = 0; i < len; i++) {
            final char c = str.charAt(i);
            if (Arrays.binarySearch(REGEXP_CHARS, c) >= 0) {
                buf.append("\\");
            }
            buf.append(c);
        }

        return buf.toString();
    }
}

Related

  1. escapeJavaString(String str)
  2. escapeJavaStyleString(StringBuilder out, String str, boolean escapeSingleQuote, boolean escapeForwardSlash)
  3. escapeQuotes(String string)
  4. escapeRegex(String regex)
  5. escapeRegexChars(String str, char... ignores)
  6. escapeRegexpSymbol(String expr)
  7. escapeReservedWord(String input)
  8. escapeSelected(String str, String chars)
  9. escapeSolr(String value)