Skip to content

Commit

Permalink
Optimize regex
Browse files Browse the repository at this point in the history
  • Loading branch information
slisaasquatch committed Jan 5, 2024
1 parent 5a8b35e commit b351c2f
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import org.apache.commons.validator.routines.EmailValidator;
import org.apache.commons.validator.routines.InetAddressValidator;
Expand All @@ -23,6 +24,10 @@ public String inferFormat(@Nonnull FormatInferrerInput input) {
},

DATE_TIME {

private final Pattern timePattern = Pattern.compile(
"^(?:[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])$");

@Override
public String inferFormat(@Nonnull FormatInferrerInput input) {
final String textValue = input.getSample().textValue();
Expand All @@ -45,8 +50,7 @@ public String inferFormat(@Nonnull FormatInferrerInput input) {
// Ignore
}
// The time format is not the same as Java's LocalTime and OffsetTime
if (textValue.matches(
"^(?:[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])$")) {
if (timePattern.matcher(textValue).matches()) {
return Consts.Formats.TIME;
}
}
Expand Down

0 comments on commit b351c2f

Please sign in to comment.