1 package com.atlassian.plugin.util;
2
3 import java.util.Collection;
4
5 import com.atlassian.annotations.Internal;
6
7 /**
8 * Utility functions for constructing regular expressions.
9 *
10 * The methods here take and return strings to faciliate composing operations without
11 * needing to compile temporary patterns.
12 */
13 @Internal
14 public class RegularExpressions
15 {
16 /**
17 * Obtain a regular expression which matches any one of a given collection of expressions.
18 *
19 * If the provided collection is empty, a regular expression which matches no string (not even
20 * the empty string) is returned.
21 *
22 * @param expressions the individual expressions to compose.
23 * @return an expression which matches when any one of expressions matches.
24 */
25 public static String anyOf(final Collection<String> expressions)
26 {
27 if (expressions.isEmpty())
28 {
29 // Nothing matches "Something followed by start of string"
30 return ".\\A";
31 }
32 else
33 {
34 // We're going to construct the following expression - spaces added for clarity, <i> are
35 // the expressions:
36 // (?:(?: <0> )|(?: <1> )|(?: <2> ... )|(?: <n-1> ))
37 // Figure out how big the resulting expression will be - break first 6 up as 1 + 5 which
38 // is what the loop below does also:
39 // ( ?:(?: <0> )|(?: <1> )|(?: <2> ... )|(?: <n-1> ))
40 int capacity = 1 + 5 * expressions.size() + 2;
41 for (final String expression : expressions)
42 {
43 capacity += expression.length();
44 }
45 // Now construct the compound regular expression
46 final StringBuilder compound = new StringBuilder(capacity);
47 compound.append('(');
48 for (final String expression : expressions)
49 {
50 compound.append(")|(?:");
51 compound.append(expression);
52 }
53 compound.append("))");
54 // Now compound starts ()|(?: and we want (?:(?:
55 compound.setCharAt(1, '?');
56 compound.setCharAt(2, ':');
57 return compound.toString();
58 }
59 }
60 }