Add UUID regex compatible with java.util.UUID. (#115)

This commit is contained in:
Andrew Fitzgerald
2016-10-18 02:17:44 -04:00
committed by Marcin Grzejszczak
parent f4a6b4f4b1
commit 8d8cdfe89b
2 changed files with 24 additions and 7 deletions

View File

@@ -36,6 +36,7 @@ class RegexPatterns {
private static final Pattern HOSTNAME_PATTERN = Pattern.compile('((http[s]?|ftp):\\/)\\/?([^:\\/\\s]+)(:[0-9]{1,5})?');
private static final Pattern EMAIL = Pattern.compile('[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}');
private static final Pattern URL = Pattern.compile('((www\\.|(http|https|ftp|news|file)+\\:\\/\\/)[_.a-z0-9-]+\\.[a-z0-9\\/_:@=.+?,##%&~-]*[^.|\\\'|\\# |!|\\(|?|,| |>|<|;|\\)])')
private static final Pattern UUID = Pattern.compile('[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}')
String onlyAlphaUnicode() {
return ONLY_ALPHA_UNICODE.pattern()
@@ -64,6 +65,10 @@ class RegexPatterns {
String url() {
return URL.pattern()
}
String uuid(){
return UUID.pattern()
}
// end::regexps[]
static String multipartParam(Object name, Object value) {

View File

@@ -66,13 +66,25 @@ class RegexPatternsSpec extends Specification {
def "should generate a regex for a number [#textToMatch] that is a match [#shouldMatch]"() {
expect:
shouldMatch == Pattern.compile(regexPatterns.number()).matcher(textToMatch).matches()
shouldMatch == Pattern.compile(regexPatterns.number()).matcher(textToMatch).matches()
where:
textToMatch || shouldMatch
'1' || true
'1.0' || true
'0.1' || true
'.1' || true
'1.' || false
textToMatch || shouldMatch
'1' || true
'1.0' || true
'0.1' || true
'.1' || true
'1.' || false
}
def "should generate a regex for a uuid [#textToMatch] that is a match [#shouldMatch]"() {
expect:
shouldMatch == Pattern.compile(regexPatterns.uuid()).matcher(textToMatch).matches()
where:
textToMatch || shouldMatch
UUID.randomUUID().toString() || true
UUID.randomUUID().toString() || true
UUID.randomUUID().toString() + "!" || false
'dog' || false
'5' || false
}
}