VerbalExpressions/purescript-verbal-expressions

Name: purescript-verbal-expressions

Owner: VerbalExpressions

Description: Purescript Regular expressions made easy

Created: 2016-01-10 18:42:43.0

Updated: 2017-11-20 07:24:19.0

Pushed: 2017-04-22 07:43:16.0

Homepage: null

Size: 29

Language: PureScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

purescript-verbal-expressions

A free monad implementation of Verbal Expressions for PureScript.

Examples
Basic testing

We can use do-notation to construct verbal expressions:

er :: VerEx
er = do
artOfLine
ssibly (anyOf "+-")
me digit
ssibly do
find "."
some digit
dOfLine

st number "42"


st number "+3.14"


st number "3."
e

The monadic interface allows us to bind the indices of capture groups to named expressions:

ern :: VerEx
ern = do
rstWord <- capture word
itespace
rd
itespace
ndAgain firstWord

This pattern matches “foo bar foo” but not “foo bar baz“.

Matching

Here, we use the result of the monadic action to return an an array of capture groups. Note that optional capturing groups are possible:

er = do
artOfLine
tPart <- capture (some digit)
oatPart <- possibly do
find "."
capture (some digit)
dOfLine
re [intPart, floatPart]

tch number "3.14"
 [Just "3", Just "14"]

tch number "42"
 [Just "42", Nothing]

tch number "."
ing
Replacing

If, instead, we return a string from the monadic action, we can use it as a replacement string (with 'named' capture groups):

Words :: String -> String
Words = replace do
rst  <- capture word
ank  <- capture (some whitespace)
cond <- capture word

placeWith (insert second <> insert blank <> insert first)

apWords "Foo   Bar"
   Foo"

Note that replaceWith is just an alias for pure.

For more examples, see the tests.

Installation
r install purescript-verbal-expressions

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.