Verified Commit 48390eea authored by Camil Staps's avatar Camil Staps 🚀

Update example to not use anchors

parent 8fc209cc
......@@ -34,27 +34,35 @@ The following regex features are supported:
import StdEnv
import Data.Functor
import Data.Maybe
import Data.Tuple
import Regex
Start = map snd <$> flip match input <$> compile rgx
Start = map (appSnd toString) <$> flip match input <$> compile rgx
where
rgx = ['^[a-zA-Z\\d][\\w\\.%+-]*@(?:[a-zA-Z\\d-]+\\.)+[a-zA-Z]{2,}$']
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.
The result will be:
If the string would not match, e.g. `['info@camilstaps..nl']`, the result would
be `Just []`.
```clean
Just [(0,"info@camilstaps.nl"),(1,"nfo@camilstaps.nl"),(2,"fo@camilstaps.nl"),(3,"o@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 contain any matches, 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`.
To only search on the entire string, add `^` and `$` anchors around the regex.
## Grammar
The following BNF grammar is recognised by `compile`:
......@@ -69,7 +77,7 @@ The following BNF grammar is recognised by `compile`:
| '(?:' <Regex> ')'
| <Literal>
<Anchor> ::= '^' | '$'
<Anchor> ::= '^' | '$' | '\b' | '\B'
<CharClass> ::= '[' ['^'] [']'] <CharRanges> ['-'] ']'
| <ShortClass>
......@@ -96,7 +104,7 @@ The following BNF grammar is recognised by `compile`:
<Special> ::= '\' | '+' | '*' | '?' | '^' | '$' | '.' | '|'
| '(' | ')' | '[' | ']' | '{' | '}'
<EscapeChar> ::= 'a' | 'b' | 'f' | 'n' | 'r' | 't' | 'v' | '\'
<EscapeChar> ::= 'a' | 'f' | 'n' | 'r' | 't' | 'v' | '\'
```
## Todo (in order of importance)
......
......@@ -3,9 +3,10 @@ module test
import StdEnv
import Data.Functor
import Data.Maybe
import Data.Tuple
import Regex
Start = map snd <$> flip match input <$> compile rgx
Start = map (appSnd toString) <$> flip match input <$> compile rgx
where
rgx = ['^[a-zA-Z\\d][\\w\\.%+-]*@(?:[a-zA-Z\\d-]+\\.)+[a-zA-Z]{2,}$']
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