public
Last active

[newLISP][PCRE]Perl互換正規表現のサンプル

  • Download Gist
hostcvt.lsp
Common Lisp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#!/usr/bin/env newlisp
 
# change all "foo.bar.com" style names in the input stream
# into "foo.bar.com [204.148.40.9]" (or whatever) instead
 
# Original Source (perl) - http://docstore.mik.ua/orelly/perl4/cook/ch06_05.htm
 
# Usage: newlisp hostcvt.lsp < /etc/hosts
 
(define (echo (in 0) (out 1))
(let (buf)
(while (read in buf 0x400)
(write out buf))
(if (string? out) out true)))
 
;; Read stdin into string
(setq TEXT (echo 0 ""))
 
(replace
{ # substitute this:
( # capture the hostname in $1
(?: # these parens for grouping only
(?! [-_] ) # next mustn't be underbar or dash
[\w-] + # one or more alphanums or dashes
\. # and the domain dot
) + # now repeat that whole thing a bunch of times
(?= [a-z] ) # next must be a letter
[\w-] + # one or more alphanums or dashes
) # end of $1 capture
}
TEXT
(format "%s [%s]" $1 (or (net-lookup $1) "???"))
(| 1 8) ; (i)PCRE_CASELESS, (x)PCRE_EXTENDED
)
 
(print TEXT)
 
(exit)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.