On this page:
Functional Reg  Exp
1.0

Functional RegExp🔗ℹ

Simon Johnston <johnstonskj@gmail.com>

 (require rx) package: rx

Regular expressions are wonderful tools and very powerful; however, complex expressions can be hard to write and harder to debug. The goal of this crate is to provide a set of functions that compose a regular expression as a set of strings. While this approach is more verbose, as can be seen in the examples below, it is more readable and easier to compose.

"((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|

(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)(:[0-9]{0,5})?(#[\w]*)?

((?:\/[\+~%\/.\w\-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)"

Example:
> (rx/group
    (rx/or-group
      (rx/and
        (rx/group
          (rx/repeat rx/match-alpha #:lower 3 #:upper 9)
            ":"
            (rx/nc-group "//" #:repeat 'optional))
        (rx/nc-group
          (rx/match "-;:&=\\+\\$,\\w" #:repeat 'one-or-more) "@"
          #:repeat 'optional)
        (rx/match rx/range-alnum ".-"
          #:repeat 'one-or-more))
      (rx/and
        (rx/nc-group
          (rx/or ":www."
                 (rx/and
                   (rx/match rx/range-alnum ".-"
                             #:repeat 'one-or-more)
                   "@")))
        (rx/match rx/range-alnum ".-"
                  #:repeat 'one-or-more)))
    (rx/group ":" (rx/repeat rx/match-digit #:lower 0 #:upper 5) #:repeat 'optional)
    (rx/group "#" (rx/* (rx/match "\\w")) #:repeat 'optional)
    (rx/group
      (rx/non-capture-group
        "/"
        (rx/match "+~%/.\\w-_" #:repeat 'zero-or-more)
        #:repeat 'optional)
      (rx/? "\\?")
      (rx/nc-group (rx/match "-+=&;%@.\\w_" #:repeat 'zero-or-more))
      (rx/? "#")
      (rx/nc-group
        (rx/match ".!/\\w" #:repeat 'zero-or-more))
      #:repeat 'optional))

"((([a-zA-Z]{3,9}:(?://)?)(?:[-;:&=\\+\\$,\\w]+@)?[a-zA-Z0-9.-]+|(?::www.|[a-zA-Z0-9.-]+@)[a-zA-Z0-9.-]+)(:[0-9]{0,5})?(#[\\w]*)?((?:/[+~%/.\\w-_]*)?\\??(?:[-+=&;%@.\\w_]*)#?(?:[.!/\\w]*))?)"