23
23
import java .util .regex .Pattern ;
24
24
25
25
/**
26
- * Package-protected helper class for {@link AntPathMatcher}.
27
- * Tests whether or not a string matches against a pattern using a regular expression.
26
+ * Package-protected helper class for {@link AntPathMatcher}. Tests whether or not a string matches against a pattern
27
+ * using a regular expression.
28
28
*
29
- * <p>The pattern may contain special characters: '*' means zero or more characters;
30
- * '?' means one and only one character; '{' and '}' indicate a URI template pattern.
29
+ * <p>The pattern may contain special characters: '*' means zero or more characters; '?' means one and only one
30
+ * character; '{' and '}' indicate a URI template pattern.
31
31
*
32
32
* @author Arjen Poutsma
33
33
* @since 3.0
@@ -36,6 +36,8 @@ class AntPatchStringMatcher {
36
36
37
37
private static final Pattern GLOB_PATTERN = Pattern .compile ("\\ ?|\\ *|\\ {([^/]+?)\\ }" );
38
38
39
+ private static final String DEFAULT_VARIABLE_PATTERN = "(.*)" ;
40
+
39
41
private final Pattern pattern ;
40
42
41
43
private String str ;
@@ -65,13 +67,24 @@ else if ("*".equals(match)) {
65
67
patternBuilder .append (".*" );
66
68
}
67
69
else if (match .startsWith ("{" ) && match .endsWith ("}" )) {
68
- patternBuilder .append ("(.*)" );
69
- variableNames .add (m .group (1 ));
70
+ int colonIdx = match .indexOf (':' );
71
+ if (colonIdx == -1 ) {
72
+ patternBuilder .append (DEFAULT_VARIABLE_PATTERN );
73
+ variableNames .add (m .group (1 ));
74
+ }
75
+ else {
76
+ String variablePattern = match .substring (colonIdx + 1 , match .length () - 1 );
77
+ patternBuilder .append ('(' );
78
+ patternBuilder .append (variablePattern );
79
+ patternBuilder .append (')' );
80
+ String variableName = match .substring (1 , colonIdx );
81
+ variableNames .add (variableName );
82
+ }
70
83
}
71
84
end = m .end ();
72
85
}
73
86
patternBuilder .append (quote (pattern , end , pattern .length ()));
74
- return Pattern .compile (patternBuilder .toString ());
87
+ return Pattern .compile (patternBuilder .toString ());
75
88
}
76
89
77
90
private String quote (String s , int start , int end ) {
0 commit comments