Commit 042b7f0e authored by Camil Staps's avatar Camil Staps 🐧

Readme & example

parent d8635595
......@@ -4,5 +4,56 @@ Pure [Clean][] regex library
Copyright © 2016 [Camil Staps][camilstaps]. Licensed under GPL v3.0, see
the `LICENSE` file for more details.
## Features
```clean
:: Regex = ...
instance toString Regex
compile :: [Char] -> Maybe Regex
match :: Regex [Char] -> [(Position, [Char])] // :: Position :== Int
```
The following regex features are supported:
* Literals
* Non-matching groups (`(?:..)`)
* Alternation (`|`)
* Repetition: `?`, `+`, `*`, `{n}`, `{n,}`, `{m,n}` (all with lazy variants)
* Character classes: `[..]` or `[^..]` where `..` contains characters,
character ranges and shorthand character classes
* Anchors: `^` and `$`
* Shorthand character classes: `.`, `\d`, `\D`, `\w`, `\W`, `\s`, `\S`
* Escape sequences: `\a`, `\b`, `\f`, `\n`, `\r`, `\t`, `\v`, `\\`, `\nnn`
(octal) and `\xhh` (hexadecimal)
## Example (`test.icl`)
```clean
import StdEnv
import Data.Functor
import Data.Maybe
import Regex
Start = map snd <$> flip match input <$> compile rgx
where
rgx = ['^[a-zA-Z\\d][\\w\\.%+-]*@(?:[a-zA-Z\\d-]+\\.)+[a-zA-Z]{2,}$']
input = ['info@camilstaps.nl']
```
This regex, taken from [http://www.regular-expressions.info/email.html](),
matches most email addresses (and rejects most not-email addresses).
Hence, the result will be `Just [['info@camilstaps.nl']]`: `Just`, since we're
fmapping the result of `compile`, and a singleton list since the result of
`match` could be the empty list as well.
If the string would not match, e.g. `['info@camilstaps..nl']`, the result would
be `Just []`.
If the regex would not compile (try adding a `{` at the end), the result would
be `Nothing`.
[camilstaps]: https://camilstaps.nl
[Clean]: http://clean.cs.ru.nl
module test
import StdEnv
import Data.Functor
import Data.Maybe
import Regex
Start = (toString rgx, match rgx string)
Start = map snd <$> flip match input <$> compile rgx
where
(Just rgx) = compile ['\\s.*[ld]']
string = ['hello world']
rgx = ['^[a-zA-Z\\d][\\w\\.%+-]*@(?:[a-zA-Z\\d-]+\\.)+[a-zA-Z]{2,}$']
input = ['info@camilstaps.nl']
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment