summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java
blob: a73292c94ca7f968ad0492d1bf152646dd91fcf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package bjc.utils.funcutils;

/**
 * Utility methods for operations on strings
 * 
 * @author ben
 *
 */
public class StringUtils {

	/**
	 * Check if a string consists only of one or more matches of a regular
	 * expression
	 * 
	 * @param input
	 *            The string to check
	 * @param regex
	 *            The regex to see if the string only contains matches of
	 * @return Whether or not the string consists only of multiple matches
	 *         of the provided regex
	 */
	public static boolean containsOnly(String input, String regex) {
		/*
		 * This regular expression is fairly simple.
		 * 
		 * First, we match the beginning of the string. Then, we start a
		 * non-capturing group whose contents are the passed in regex. That
		 * group is then matched one or more times and the pattern matches
		 * to the end of the string
		 */
		if (input == null) {
			throw new NullPointerException("Input must not be null");
		} else if (regex == null) {
			throw new NullPointerException("Regex must not be null");
		}

		return input.matches("\\A(?:" + regex + ")+\\Z");
	}

}