Skip to content

Commit b351c2f

Browse files
committed
Optimize regex
1 parent 5a8b35e commit b351c2f

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/main/java/com/saasquatch/jsonschemainferrer/BuiltInFormatInferrer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.time.LocalDate;
44
import java.time.ZonedDateTime;
5+
import java.util.regex.Pattern;
56
import javax.annotation.Nonnull;
67
import org.apache.commons.validator.routines.EmailValidator;
78
import org.apache.commons.validator.routines.InetAddressValidator;
@@ -23,6 +24,10 @@ public String inferFormat(@Nonnull FormatInferrerInput input) {
2324
},
2425

2526
DATE_TIME {
27+
28+
private final Pattern timePattern = Pattern.compile(
29+
"^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](?:\\.\\d+)?(?:Z|[+-](?:0[0-9]|2[0-3]):[0-5][0-9])$");
30+
2631
@Override
2732
public String inferFormat(@Nonnull FormatInferrerInput input) {
2833
final String textValue = input.getSample().textValue();
@@ -45,8 +50,7 @@ public String inferFormat(@Nonnull FormatInferrerInput input) {
4550
// Ignore
4651
}
4752
// The time format is not the same as Java's LocalTime and OffsetTime
48-
if (textValue.matches(
49-
"^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](?:\\.\\d+)?(?:Z|[+-](?:0[0-9]|2[0-3]):[0-5][0-9])$")) {
53+
if (timePattern.matcher(textValue).matches()) {
5054
return Consts.Formats.TIME;
5155
}
5256
}

0 commit comments

Comments
 (0)