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
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
A free monad implementation of Verbal Expressions for PureScript.
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“.
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
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.
r install purescript-verbal-expressions