Skip to content

Instantly share code, notes, and snippets.

@SimonMeskens
Last active March 20, 2022 04:09
Show Gist options
  • Save SimonMeskens/958154090956ec36f728334743a05606 to your computer and use it in GitHub Desktop.
Save SimonMeskens/958154090956ec36f728334743a05606 to your computer and use it in GitHub Desktop.

Design challenge: design or share a regular expression DSL

Design a syntax for an alternative to classic regex. You can also share interesting existing solutions. An example of a regular expression to try implementing is the JSON number spec:

n = /-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/

All the examples here are licensed CC0. If you would like to share a solution for addition to this gist (and later a repository), send it to me and mention that you license it under CC0. If you would like to be attributed, mention under what name.

Existing solutions

Example: Natural language

by Simon Meskens

Here is an example that tries to be the opposite of classic regex: fully verbose to explain the idea as best as it can. No concern was had for ease of authoring however. For the actual syntax, compatibility with Markdown was chosen.

# JSON Number
sequence of
* *Sign?*: possibly '-'
* *Integer*: either
  - 0
  - (1 to 9) followed by zero or more digits
* *Fraction?*: possibly ('.' followed by one or more digits)
* *Exponent?*: possibly sequence of
  1. 'e' or 'E'
  2. possibly ('+' or '-')
  3. one or more digits

This example implements the JSON number regex in Red's Parse dialect:

digit: charset [#"0" - #"9"]
nonzero: charset [#"1" - #"9"]
parse text [
    opt "-"
    [ "0" | nonzero any digit ]
    opt [ "." some digit ]
    opt [
        "e" ;-- parse is case-insensitive by default
        opt [ "+" | "-" ]
        some digit
    ]
]

Courtesy of @ALANVF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment