xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/util/RegexUtils.java (revision 5d9f3aa0ca3da3a714233f987fa732f62c0965f6)
1b5840353SAdam Hornáček /*
2b5840353SAdam Hornáček  * CDDL HEADER START
3b5840353SAdam Hornáček  *
4b5840353SAdam Hornáček  * The contents of this file are subject to the terms of the
5b5840353SAdam Hornáček  * Common Development and Distribution License (the "License").
6b5840353SAdam Hornáček  * You may not use this file except in compliance with the License.
7b5840353SAdam Hornáček  *
8b5840353SAdam Hornáček  * See LICENSE.txt included in this distribution for the specific
9b5840353SAdam Hornáček  * language governing permissions and limitations under the License.
10b5840353SAdam Hornáček  *
11b5840353SAdam Hornáček  * When distributing Covered Code, include this CDDL HEADER in each
12b5840353SAdam Hornáček  * file and include the License file at LICENSE.txt.
13b5840353SAdam Hornáček  * If applicable, add the following below this CDDL HEADER, with the
14b5840353SAdam Hornáček  * fields enclosed by brackets "[]" replaced with your own identifying
15b5840353SAdam Hornáček  * information: Portions Copyright [yyyy] [name of copyright owner]
16b5840353SAdam Hornáček  *
17b5840353SAdam Hornáček  * CDDL HEADER END
18b5840353SAdam Hornáček  */
19b5840353SAdam Hornáček 
20b5840353SAdam Hornáček /*
21b5840353SAdam Hornáček  * Copyright (c) 2017, Chris Fraire <cfraire@me.com>.
22b5840353SAdam Hornáček  */
23*9805b761SAdam Hornáček package org.opengrok.indexer.util;
24b5840353SAdam Hornáček 
25b5840353SAdam Hornáček /**
26b5840353SAdam Hornáček  * Represents a container for regex-related utility methods.
27b5840353SAdam Hornáček  */
28b5840353SAdam Hornáček public class RegexUtils {
29b5840353SAdam Hornáček 
30b5840353SAdam Hornáček     /** Private to enforce singleton. */
RegexUtils()31b5840353SAdam Hornáček     private RegexUtils() {
32b5840353SAdam Hornáček     }
33b5840353SAdam Hornáček 
34b5840353SAdam Hornáček     /**
35b5840353SAdam Hornáček      * Gets a zero-width expression intended to follow a character match that
36b5840353SAdam Hornáček      * asserts that the character either does not follow a backslash escape or
37b5840353SAdam Hornáček      * follows an even number¹ of backslash escapes.
38b5840353SAdam Hornáček      * <p>
39b5840353SAdam Hornáček      * ¹"even number" is limited to 2,4,6 because Java look-behind is not
40b5840353SAdam Hornáček      * variable length but instead must have a definite upper bound in the
41b5840353SAdam Hornáček      * regex definition.
42b5840353SAdam Hornáček      * @return a defined expression:
43b5840353SAdam Hornáček      * <pre>
44b5840353SAdam Hornáček      * {@code
45b5840353SAdam Hornáček      * ((?<=^.)|(?<=[^\\].)|(?<=^(\\\\){1,3}.)|(?<=[^\\](\\\\){1,3}.))
46b5840353SAdam Hornáček      * }
47b5840353SAdam Hornáček      * </pre>
48b5840353SAdam Hornáček      * (Edit above and paste below [in NetBeans] for easy String escaping.)
49b5840353SAdam Hornáček      */
getNotFollowingEscapePattern()50b5840353SAdam Hornáček     public static String getNotFollowingEscapePattern() {
51b5840353SAdam Hornáček         return "((?<=^.)|(?<=[^\\\\].)|(?<=^(\\\\\\\\){1,3}.)|(?<=[^\\\\](\\\\\\\\){1,3}.))";
52b5840353SAdam Hornáček     }
53b5840353SAdam Hornáček }
54