Coder Social home page Coder Social logo

justinwoo / purescript-verbal-expressions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from verbalexpressions/purescript-verbal-expressions

0.0 3.0 0.0 30 KB

Purescript Regular expressions made easy

License: MIT License

PureScript 100.00%

purescript-verbal-expressions's Introduction

purescript-verbal-expressions

A free monad implementation of Verbal Expressions for PureScript.

Examples

Basic testing

We can use do-notation to construct verbal expressions:

number :: VerEx
number = do
  startOfLine
  possibly (anyOf "+-")
  some digit
  possibly do
    find "."
    some digit
  endOfLine

> test number "42"
true

> test number "+3.14"
true

> test number "3."
false

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

pattern :: VerEx
pattern = do
  firstWord <- capture word
  whitespace
  word
  whitespace
  findAgain 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 array of capture groups. Note that optional capturing groups are possible:

number = do
  startOfLine
  intPart <- capture (some digit)
  floatPart <- possibly do
    find "."
    capture (some digit)
  endOfLine
  pure [intPart, floatPart]

> match number "3.14"
Just [Just "3", Just "14"]

> match number "42"
Just [Just "42", Nothing]

> match number "."
Nothing

Replacing

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

swapWords :: String -> String
swapWords = replace do
  first  <- capture word
  blank  <- capture (some whitespace)
  second <- capture word

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

> swapWords "Foo   Bar"
"Bar   Foo"

Note that replaceWith is just an alias for pure.

For more examples, see the tests.

Installation

bower install purescript-verbal-expressions

purescript-verbal-expressions's People

Contributors

sharkdp avatar paulyoung avatar lpmi-13 avatar

Watchers

James Cloos avatar Justin Woo avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.