Java String Replace replaceWildcards(String wildcardPattern)

Here you can find the source of replaceWildcards(String wildcardPattern)

Description

replace Wildcards

License

Open Source License

Declaration

public static String replaceWildcards(String wildcardPattern) 

Method Source Code


//package com.java2s;
/*//from w  ww. jav  a 2 s  .c o m
 * Data Scrambler, Data Generation API
 * Copyright (c) 2015, Sergiu Prutean. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 */

import java.util.*;

public class Main {
    private static final Set<Character> PREFIXED_CHAR_SET = new HashSet<Character>(
            Arrays.asList('+', '(', ')', '^', '$', '.', '{', '}', '[', ']', '|', '\\'));

    public static String replaceWildcards(String wildcardPattern) {
        final StringBuilder builder = new StringBuilder();
        builder.append('^');
        boolean replaced = false;
        final int length = wildcardPattern.length();
        for (int i = 0; i < length; ++i) {
            final char ch = wildcardPattern.charAt(i);
            if (ch == '*') {
                builder.append(".*");
                replaced = true;
            } else if (ch == '?') {
                builder.append(".");
                replaced = true;
            } else if (PREFIXED_CHAR_SET.contains(ch)) {
                builder.append('\\').append(ch);
            } else {
                builder.append(ch);
            }
        }
        builder.append('$');
        return replaced ? builder.toString() : wildcardPattern;
    }
}

Related

  1. replaceNonPrintableAsciiCharacters(String str)
  2. replaceOnce(String template, String placeholder, String replacement)
  3. replaceSign(String sourceStr, char startc, char endc, String replaceStr)
  4. replaceSpace(String str, int actualLength)
  5. replaceUnsafeNamespaceDelimiters(String input)
  6. searchAndReplace(String source, String search, String replace)
  7. strReplaceOld(String strText, String strFrom, String strTo, boolean bMultiple)
  8. subStitute(String value, String pattern, String replacement)
  9. substituteIgnoreCase(String s, String pattern, String replacement)