Magic Pipes
Documentation
Login

mpre {sre|-p pcre} [-o output-proc]

The argument must be a valid SRE; or, if -p is used, a POSIX regexp.

Reads in lines of text from stdin and converts them to s-expressions by applying the regular expression. Lines that do not match the regexp are ignored.

If -o is specified, then the expression must be a single-argument procedure which is applied to each irregex match object to generate the output s-expression. If not, then a default is used which has the following behaviour:

If the regexp has no captures, then the entire matching string is returned.

If it has only numbered submatches, then a list of the submatches is returned.

If it has named submatches, then an alist of them is returned, and any numbered submatches are ignored.

As with mpmap, the output procedure is welcome to return other than a single value, which will be handled appropriately.

Examples

Default output, numbered submatches

$ printf 'Hello world\nHello [world]\nDear [bob], I love you\n' |\
        mpre '(seq ($ (* any)) "[" ($ (* any)) "]" ($ (* any)))'
("Hello " "world" "")
("Dear " "bob" ", I love you")

Default output, named submatches

$ printf 'Hello world\nHello [world]\nDear [bob], I love you\n' |\
        mpre '(seq (=> pre (* any)) "[" (=> bracketed (* any)) "]" (=> post (* any)))'
((pre . "Hello ") (bracketed . "world") (post . ""))
((pre . "Dear ") (bracketed . "bob") (post . ", I love you"))

Parsing output of ls (as if we didn't have mpls)

$ ls -l *.scm | \
       mpre '(seq (=> perms (= 10 any)) (* space) (=> linkcount integer) (* space) (=> user symbol) (* space) (=> group symbol) (* space) (=> size integer) (* space) (=> modtime symbol (* space) integer (* space) integer ":" integer) (* space) (=> name (* any)))'
((perms . "-rw-r--r--") (linkcount . "1") (user . "alaric") (group . "users") (size . "7397") (modtime . "Aug 16 14:30") (name . "magic-pipes.scm"))
((perms . "-rw-r--r--") (linkcount . "1") (user . "alaric") (group . "users") (size . "5340") (modtime . "Aug 16 14:54") (name . "mpfold.scm"))
((perms . "-rw-r--r--") (linkcount . "1") (user . "alaric") (group . "users") (size . "2844") (modtime . "Aug 16 15:36") (name . "mpre.scm"))

Reading each line of input into a string

$ printf 'Hello\nWorld\nHow\nAre\nYou\nToday?' | mpre '(* any)'
"Hello"
"World"
"How"
"Are"
"You"
"Today?"