Skip to content

Package: CapturingGroups

CapturingGroups

nameinstructionbranchcomplexitylinemethod
CapturingGroups()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
captureTheGroups(String, String)
M: 0 C: 30
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%

Coverage

1: package capturinggroups;
2:
3: import java.util.ArrayList;
4: import java.util.List;
5: import java.util.regex.Matcher;
6: import java.util.regex.Pattern;
7:
8: /**
9: * This class makes possible to capture groups.
10: *
11: * @author Sandra und Patrick
12: *
13: */
14: public class CapturingGroups {
15:
16:         /**
17:          * This attribute represents the list with the results.
18:          */
19:         private final List<String> resultList = new ArrayList<String>();
20:
21:         /**
22:          * This method capture the groups.
23:          *
24:          * @param expression
25:          * the expression is the input string.
26:          * @param regExp
27:          * this is the definition of groups.
28:          * @return a list of captured groups.
29:          */
30:         public List<String> captureTheGroups(final String expression, final String regExp) {
31:
32:                 final Pattern pattern = Pattern.compile(regExp);
33:                 final Matcher matcher = pattern.matcher(expression);
34:                 final boolean exists = matcher.find();
35:•                if (exists) {
36:•                        for (int i = 0; i <= matcher.groupCount(); i++) {
37:                                 this.resultList.add(matcher.group(i));
38:                         }
39:                 }
40:                 return this.resultList;
41:         }
42: }