perlre

2020-07-15

s///, m//, d// - regular expression API for CL-PPCRE and CL-INTERPOL

Upstream URL

github.com/jschatzer/perlre

Author

<schatzer.johann@gmail> using idea and code from LET-OVER-LAMBDA

License

BSD Simplified --- the same as let-over-lambda
README

perlre

Regular expression API using CL-PPCRE and CL-INTERPOL with operators known from sed or perl, m// and s/// plus a similar one, d// for split.

Can be useful for code brevity in regex heavy programs.

idea and code from Doug Hoyte's book Let Over Lambda and quicklisp-package let-over-lambda

Synopsis:

1)
(set-dispatch-macro-character #\# #\~ 'perlre:|#~-reader|)

2)
(#~s/regex/substitution/imsxge string)
(#~m/regex/[modifier] string)
(#~d/regex/[modifier] string) 

(ifmatch test then else)
(whenmatch test conseq*)
(match string clause*) ; a clause (test conseq) e.g (#~m/(regex)/ (list $1))


Some examples:

  ; output of both is "h*a*nn*a*" 
  (let ((stg "hanna")
        (reg "(A)")
        (sub "*\\1*"))

    ; perlre
    (#~s/reg/sub/ig stg)

    ; cl-ppcre
    (ppcre:regex-replace-all (ppcre:create-scanner reg :case-insensitive-mode t) stg sub))
  



  ; -> 2
  (match "2012-11-04" 
    (#~m'abc' 1)
    (t 2))

  ;    HELLO
  ; -> 2016
  (match "2012-11-04" 
    (#~m'abc' 1)
    (#~m§(2012)-11-(04)§ (print 'hello) (+ (parse-integer $1) (parse-integer $2)))
    (t 2))

  ;"04/2012"
  (pre:match "2012-11-04"
    (#~m'abc' 1)
    (#~m§(\d{4})-.+-(..)§ (format nil "~a/~a" $2 $1))
    (t 2))

See test.lisp for other examples.

;;;; 1.5.19 ;;;;;;;;; vim syntax/color in ~/.vim/sytax/lisp.vim I have this regex to see the whole expressin in green syn match lispPerlre "#~s(.).{-}\1.{-}\1\w*" and hi lispPerlre ctermfg=green in ./vim/colors/self.vim

in vimre syntyx color

Dependencies (5)

  • cl-interpol
  • cl-ppcre
  • let-over-lambda
  • prove
  • trivia

Dependents (1)

  • GitHub
  • Quicklisp