function escapeRegEx() v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 24 Dec 2013 00:15:04 +0100
branchv_0
changeset 460b05bc13aadd
parent 45 ce33736066b1
child 47 92b83789330a
function escapeRegEx()
java/sql-dk/src/info/globalcode/sql/dk/Functions.java
java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Tue Dec 24 00:12:53 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Tue Dec 24 00:15:04 2013 +0100
     1.3 @@ -133,4 +133,29 @@
     1.4  		Arrays.fill(array, ch);
     1.5  		return new String(array);
     1.6  	}
     1.7 +
     1.8 +	/**
     1.9 +	 * @param original any text
    1.10 +	 * @return escaped text that can be used as part of an regular expression, matches the original
    1.11 +	 * text
    1.12 +	 */
    1.13 +	public static String escapeRegEx(String original) {
    1.14 +		StringBuilder escaped = new StringBuilder(original.length() * 3);
    1.15 +
    1.16 +		escaped.append("\\Q"); // start quotation
    1.17 +		for (int i = 0; i < original.length(); i++) {
    1.18 +			char ch = original.charAt(i);
    1.19 +			if (ch == 'E' && i > 0 && original.charAt(i - 1) == '\\') {
    1.20 +				escaped.append(ch); // this unintentionally ends quotation
    1.21 +				escaped.append("\\\\E"); // insert escaped \E (eaten before)
    1.22 +				escaped.append("\\Q"); // re-start quotation
    1.23 +			} else {
    1.24 +				escaped.append(ch);
    1.25 +			}
    1.26 +
    1.27 +		}
    1.28 +		escaped.append("\\E"); // end quotation
    1.29 +
    1.30 +		return escaped.toString();
    1.31 +	}
    1.32  }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java	Tue Dec 24 00:15:04 2013 +0100
     2.3 @@ -0,0 +1,59 @@
     2.4 +/**
     2.5 + * SQL-DK
     2.6 + * Copyright © 2013 František Kučera (frantovo.cz)
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, either version 3 of the License, or
    2.11 + * (at your option) any later version.
    2.12 + *
    2.13 + * This program is distributed in the hope that it will be useful,
    2.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    2.16 + * GNU General Public License for more details.
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License
    2.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    2.20 + */
    2.21 +package info.globalcode.sql.dk;
    2.22 +
    2.23 +import java.util.regex.Matcher;
    2.24 +import java.util.regex.Pattern;
    2.25 +import org.testng.annotations.Test;
    2.26 +import static org.testng.Assert.*;
    2.27 +
    2.28 +/**
    2.29 + *
    2.30 + * @author Ing. František Kučera (frantovo.cz)
    2.31 + */
    2.32 +public class FunctionsTest {
    2.33 +
    2.34 +	@Test
    2.35 +	public void testEscapeRegEx() {
    2.36 +		for (String original : new String[]{"abcd", "1234", "xxx", "\\Eescape\\Q", "\\Qescape\\E", "abc\\Eescape\\Qdef.", ".", ""}) {
    2.37 +			String patternString = Functions.escapeRegEx(original);
    2.38 +			System.out.println(original + " → " + patternString);
    2.39 +
    2.40 +			Pattern pattern = Pattern.compile(patternString);
    2.41 +
    2.42 +			String testString;
    2.43 +			Matcher m;
    2.44 +
    2.45 +			testString = original;
    2.46 +			m = pattern.matcher(testString);
    2.47 +			assertTrue(m.matches(), "Pattern does not match original string: " + testString);
    2.48 +
    2.49 +			testString = original + "x";
    2.50 +			m = pattern.matcher(testString);
    2.51 +			assertFalse(m.matches(), "Pattern matches wrong string: " + testString);
    2.52 +
    2.53 +			testString = "x" + original;
    2.54 +			m = pattern.matcher(testString);
    2.55 +			assertFalse(m.matches(), "Pattern matches wrong string: " + testString);
    2.56 +
    2.57 +			testString = (original + "ab").substring(1);
    2.58 +			m = pattern.matcher(testString);
    2.59 +			assertFalse(m.matches(), "Pattern matches wrong string: " + testString);
    2.60 +		}
    2.61 +	}
    2.62 +}