Created
April 27, 2012 07:31
-
-
Save niklasl/2506955 to your computer and use it in GitHub Desktop.
Validate CURIE regexp patterns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
valid_curies = [ | |
"pfx:abc", | |
":", | |
"pfx:", | |
"abc", | |
":abc", | |
"", | |
"pfx:/abc", | |
"pfx:/", | |
":/", | |
] | |
non_curies = [ | |
"pfx://abc", | |
"pfx://", | |
"://", | |
"/", | |
] | |
def validate_curie_pattern(pattern): | |
print("Using pattern: " + pattern) | |
# replace special XSD character groups with perl5-compatible groups | |
curie_regex = re.compile(pattern. | |
replace(r'\i-[:]', r'_A-Za-z'). | |
replace(r'\c-[:]', r'-._:A-Za-z0-9')) | |
failed = 0 | |
for valid in valid_curies: | |
if not curie_regex.match(valid): | |
print("Failed to validate CURIE: " + valid) | |
failed += 1 | |
for non in non_curies: | |
if not curie_regex.match(non): | |
print("Mistakenly matches non-CURIE: " + non) | |
failed += 1 | |
print("Failed: %s" % failed if failed else "Ok") | |
PATTERNS = { | |
'curr': r'(([\i-[:]][\c-[:]]*)?:)?(/[^\s/]|[^\s/])[^\s]*', | |
'alt': r'(([\i-[:]][\c-[:]]*)?:)?(/[^\s/][^\s]*|[^\s/][^\s]*|[^\s]?)', | |
} | |
from sys import argv | |
name = None if len(argv) == 1 else argv[1] | |
pattern = PATTERNS.get(name) | |
if pattern: | |
validate_curie_pattern(pattern) | |
else: | |
print("Provide the name to use. One of: %s" % ', '.join(PATTERNS)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment