Skip to content

Instantly share code, notes, and snippets.

@cellularmitosis
Last active October 15, 2019 07:29
Show Gist options
  • Save cellularmitosis/6e3397565f9cd0b4edff33c8821834e1 to your computer and use it in GitHub Desktop.
Save cellularmitosis/6e3397565f9cd0b4edff33c8821834e1 to your computer and use it in GitHub Desktop.
Clojure symbol regex
$ ./symbol-regex.py
([a-zA-Z\.\*+!\-_?$%&=<>][a-zA-Z0-9\.\*+!\-_?$%&=<>]+\/[a-zA-Z\.\*+!\-_?$%&=<>][a-zA-Z0-9\.\*+!\-_?$%&=<>]+)|([a-zA-Z\.\*+!\-_?$%&=<>][a-zA-Z0-9\.\*+!\-_?$%&=<>]+\/[a-zA-Z\.\*+!\-_?$%&=<>\/])|([a-zA-Z\.\*+!\-_?$%&=<>]\/[a-zA-Z\.\*+!\-_?$%&=<>][a-zA-Z0-9\.\*+!\-_?$%&=<>]+)|([a-zA-Z\.\*+!\-_?$%&=<>]\/[a-zA-Z\.\*+!\-_?$%&=<>\/])|([a-zA-Z\.\*+!\-_?$%&=<>][a-zA-Z0-9\.\*+!\-_?$%&=<>]+)|([a-zA-Z\.\*+!\-_?$%&=<>\/])
#!/usr/bin/env python
# a program to generate the regex for a symbol in clojure.
"""
character classes:
- `a` (alpha): `[a-zA-Z]`
- `n` (num): `[0-9]`
- `s` special: `[\.\*+!\-_?$%&=<>]`
- `l` slash: `\/`
- `c` colon: `:`
a symbol can be:
- multi-char prefix, multi-char name: `[as][ans]+l[as][ans]+`
- multi-char prefix, single-char name: `[as][ans]+l[asl]`
- single-char prefix, multi-char name: `[as]l[as][ans]+`
- single-char prefix, single-char name: `[as]l[asl]`
- multi-char name: `[as][ans]+`
- single-char name: `[asl]`
"""
import sys
# character classes:
a = "a-zA-Z"
n = "0-9"
s = "\.\*+!\-_?$%&=<>"
l = "\/"
# a symbol can be:
sys.stdout.write("(")
# a multi-character prefix, multi-character name: `[as][ans]+l[as][ans]+`
sys.stdout.write("[%s%s][%s%s%s]+%s[%s%s][%s%s%s]+" % (a,s, a,n,s, l, a,s, a,n,s))
# or
sys.stdout.write(")|(")
# a multi-character prefix, single-character name: `[as][ans]+l[asl]`
sys.stdout.write("[%s%s][%s%s%s]+%s[%s%s%s]" % (a,s, a,n,s, l, a,s,l))
# or
sys.stdout.write(")|(")
# a single-character prefix, multi-character name: `[as]l[as][ans]+`
sys.stdout.write("[%s%s]%s[%s%s][%s%s%s]+" % (a,s, l, a,s, a,n,s))
# or
sys.stdout.write(")|(")
# a single-character prefix, single-character name: `[as]l[asl]`
sys.stdout.write("[%s%s]%s[%s%s%s]" % (a,s, l, a,s,l))
# or
sys.stdout.write(")|(")
# a multi-character name: `[as][ans]+`
sys.stdout.write("[%s%s][%s%s%s]+" % (a,s, a,n,s))
# or
sys.stdout.write(")|(")
# a single-character name: `[asl]`
sys.stdout.write("[%s%s%s]" % (a, s, l))
sys.stdout.write(")\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment