RegexUtils.java

1
package com.renomad.minum.testing;
2
3
import java.util.regex.Matcher;
4
import java.util.regex.Pattern;
5
6
/**
7
 * Handy helpers to make regular expression marginally
8
 * easier / more efficient, etc.
9
 */
10
public final class RegexUtils {
11
12
    /**
13
     * Helper to find a value in a string using a
14
     * Regex. Note, this is not nearly as performant, since
15
     * each call to this method will compile the regular
16
     * expression.
17
     * @return returns the first match found, or an empty string
18
     */
19
    public static String find(String regex, String data) {
20
        Pattern pattern = Pattern.compile(regex);
21
        Matcher matcher = pattern.matcher(data);
22 1 1. find : negated conditional → KILLED
        return matcher.find() ? matcher.group(0) : "";
23
    }
24
25
    private RegexUtils() {}
26
27
    /**
28
     * Returns whether the regular expression matched the data
29
     * Note: This is a poor-performance method, mainly used
30
     * as a quick helper where performance concerns don't exist,since
31
     * each call to this method will compile the regular
32
     * expression.
33
     */
34
    public static boolean isFound(String regex, String data) {
35
        Pattern pattern = Pattern.compile(regex);
36
        Matcher matcher = pattern.matcher(data);
37 2 1. isFound : replaced boolean return with false for com/renomad/minum/testing/RegexUtils::isFound → KILLED
2. isFound : replaced boolean return with true for com/renomad/minum/testing/RegexUtils::isFound → KILLED
        return matcher.find();
38
    }
39
40
    /**
41
     * Find a value by regular expression, for testing
42
     * <p>
43
     * A helper method to make things it easier to find a value in a string using a
44
     * Regex. <em>This method is slow, since
45
     * each call will compile the regular
46
     * expression.</em>
47
     * </p>
48
     * <p>
49
     *     This version is similar to {@link #find(String, String)} except
50
     *     that it allows you to specify a match group by name.
51
     *     For example, here's a regex with a named match group,
52
     *     in this example the name is "namevalue":
53
     * </p>
54
     * <p>
55
     *     <pre>
56
     *         {@code "\\bname\\b=\"(?<namevalue>.*?)\""}
57
     *     </pre>
58
     * </p>
59
     * <p>
60
     *     Thus, to use it here, you would search like this:
61
     * </p>
62
     * <p>
63
     *     <pre>
64
     *         {@code find("\\bname\\b=\"(?<namevalue>.*?)\"", data, "namevalue")}
65
     *     </pre>
66
     * </p>
67
     * <p>
68
     *     To summarize: in a regex, you specify a matching group by
69
     *     surrounding it with parentheses.  To name it, you insert
70
     *     right after the opening parenthesis a question mark and
71
     *     then a string literal surrounded by angle brackets
72
     * </p>
73
     * <p><b>Important</b>: the name of the match group must be alphanumeric - do
74
     * <b>not</b> use any special characters or punctuation</p>
75
     * @return returns the first match found, or an empty string
76
     */
77
    public static String find(String regex, String data, String matchGroupName) {
78
        Pattern pattern = Pattern.compile(regex);
79
        Matcher matcher = pattern.matcher(data);
80 1 1. find : negated conditional → KILLED
        return matcher.find() ? matcher.group(matchGroupName) : "";
81
    }
82
}

Mutations

22

1.1
Location : find
Killed by : com.renomad.minum.testing.RegexUtilsTests.test_RegexUtils_QuickSearch(com.renomad.minum.testing.RegexUtilsTests)
negated conditional → KILLED

37

1.1
Location : isFound
Killed by : com.renomad.minum.testing.RegexUtilsTests.test_isFound(com.renomad.minum.testing.RegexUtilsTests)
replaced boolean return with false for com/renomad/minum/testing/RegexUtils::isFound → KILLED

2.2
Location : isFound
Killed by : com.renomad.minum.testing.RegexUtilsTests.test_isFound(com.renomad.minum.testing.RegexUtilsTests)
replaced boolean return with true for com/renomad/minum/testing/RegexUtils::isFound → KILLED

80

1.1
Location : find
Killed by : com.renomad.minum.testing.RegexUtilsTests.test_RegexUtils_SearchNamedMatchingGroup(com.renomad.minum.testing.RegexUtilsTests)
negated conditional → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0