Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Validate CURIE regexp patterns
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