Java Regex Create toRegexPattern(String pattern)

Here you can find the source of toRegexPattern(String pattern)

Description

Converts a pattern string with '*' and '?'

License

Open Source License

Parameter

Parameter Description
pattern a pattern string

Return

a regular expression pattern string

Declaration

private static String toRegexPattern(String pattern) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007, 2008 Anyware Technologies and others. All rights reserved. This program
 * and the accompanying materials are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 * //from w  ww  .  jav  a  2 s. c  om
 * ModelSearchQueryTextualExpressionMatchingHelper.java
 * 
 * Contributors:
 *      Lucas Bigeardel (Anyware Technologies) - initial API and implementation
 *      Lucas Bigeardel - added partial matching strategy
 ******************************************************************************/

public class Main {
    /**
     * Converts a pattern string with '*' and '?' into a regular expression
     * pattern string.
     * 
     * @param pattern
     *            a pattern string
     * @return a regular expression pattern string
     */
    private static String toRegexPattern(String pattern) {
        int patternLength = pattern.length();
        StringBuffer result = new StringBuffer(patternLength);

        boolean escaped = false;
        boolean quoting = false;

        for (int i = 0; i < patternLength; i++) {
            char ch = pattern.charAt(i);
            switch (ch) {
            case '*': {
                if (!escaped) {
                    if (quoting) {
                        result.append("\\E"); //$NON-NLS-1$
                        quoting = false;
                    }
                }
                result.append(".*"); //$NON-NLS-1$
                escaped = false;
                break;
            }
            case '?': {
                if (!escaped) {
                    if (quoting) {
                        result.append("\\E"); //$NON-NLS-1$
                        quoting = false;
                    }
                }
                result.append("."); //$NON-NLS-1$
                escaped = false;
                break;
            }
            case '\\': {
                if (!escaped) {
                    escaped = true;
                } else {
                    escaped = false;
                    if (quoting) {
                        result.append("\\E"); //$NON-NLS-1$
                        quoting = false;
                    }
                    result.append("\\\\"); //$NON-NLS-1$
                }
                break;
            }
            default: {
                if (!quoting) {
                    result.append("\\Q"); //$NON-NLS-1$
                    quoting = true;
                }
                if (escaped && ch != '*' && ch != '?' && ch != '\\') {
                    result.append('\\');
                }
                result.append(ch);
                escaped = (ch == '\\');
            }
            }
        }

        if (quoting) {
            result.append("\\E"); //$NON-NLS-1$
        }

        return result.toString();
    }
}

Related

  1. toRegex(String name)
  2. toRegex(String param)
  3. toRegex(String wildcard)
  4. toRegExFormat(String pattern)
  5. toRegexPattern(String filter)