2013-11-01 22:51:23 +00:00
|
|
|
module Regex where
|
|
|
|
{-| A library for working with regular expressions. It uses [the
|
|
|
|
same kind of regular expressions accepted by JavaScript](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions).
|
|
|
|
|
|
|
|
# Create
|
2014-02-10 19:08:40 +00:00
|
|
|
@docs regex, escape, caseInsensitive
|
2013-11-01 22:51:23 +00:00
|
|
|
|
2014-02-10 19:08:40 +00:00
|
|
|
# Helpful Data Structures
|
2013-11-01 22:51:23 +00:00
|
|
|
|
2014-02-10 19:08:40 +00:00
|
|
|
These data structures are needed to help define functions like [`find`](#find)
|
|
|
|
and [`replace`](#replace).
|
2013-11-01 22:51:23 +00:00
|
|
|
|
2014-02-10 19:08:40 +00:00
|
|
|
@docs HowMany, Match
|
|
|
|
|
|
|
|
# Use
|
|
|
|
@docs contains, find, replace, split
|
2013-11-01 22:51:23 +00:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
import Maybe (Maybe)
|
|
|
|
import Native.Regex
|
|
|
|
|
|
|
|
data Regex = Regex
|
|
|
|
|
2014-02-10 19:08:40 +00:00
|
|
|
{-| Escape strings to be regular expressions, making all special characters
|
|
|
|
safe. So `regex (escape "^a+")` will match exactly `"^a+"` instead of a series
|
|
|
|
of `a`’s that start at the beginning of the line.
|
2013-11-01 22:51:23 +00:00
|
|
|
-}
|
|
|
|
escape : String -> String
|
|
|
|
escape = Native.Regex.escape
|
|
|
|
|
|
|
|
{-| Create a Regex that matches patterns [as specified in JavaScript](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions#Writing_a_Regular_Expression_Pattern).
|
2014-02-10 19:08:40 +00:00
|
|
|
|
|
|
|
Be careful to escape backslashes properly! For example, `"\w"` is escaping the
|
|
|
|
letter `w` which is probably not what you want. You probably want `"\\w"`
|
|
|
|
instead, which escapes the backslash.
|
2013-11-01 22:51:23 +00:00
|
|
|
-}
|
2014-02-10 19:08:40 +00:00
|
|
|
regex : String -> Regex
|
|
|
|
regex = Native.Regex.regex
|
2013-11-01 22:51:23 +00:00
|
|
|
|
|
|
|
|
2014-02-10 19:08:40 +00:00
|
|
|
{-| Make a regex case insensitive -}
|
2013-11-01 22:51:23 +00:00
|
|
|
caseInsensitive : Regex -> Regex
|
|
|
|
caseInsensitive = Native.Regex.caseInsensitive
|
|
|
|
|
|
|
|
{-| Check to see if a Regex is contained in a string.
|
|
|
|
|
2013-11-04 16:06:48 +00:00
|
|
|
```haskell
|
2014-02-10 19:08:40 +00:00
|
|
|
contains (regex "123") "12345" == True
|
|
|
|
contains (regex "b+") "aabbcc" == True
|
2013-11-01 22:51:23 +00:00
|
|
|
|
2014-02-10 19:08:40 +00:00
|
|
|
contains (regex "789") "12345" == False
|
|
|
|
contains (regex "z+") "aabbcc" == False
|
2013-11-04 16:06:48 +00:00
|
|
|
```
|
2013-11-01 22:51:23 +00:00
|
|
|
-}
|
|
|
|
contains : Regex -> String -> Bool
|
|
|
|
contains = Native.Regex.contains
|
|
|
|
|
|
|
|
{-| A `Match` represents all of the details about a particular match in a string.
|
|
|
|
Here are details on each field:
|
|
|
|
|
|
|
|
* `match` — the full string of the match.
|
2014-02-10 19:08:40 +00:00
|
|
|
* `submatches` — a regex might have [subpatterns, surrounded by
|
2013-11-01 22:51:23 +00:00
|
|
|
parentheses](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Parenthesized_Substring_Matches).
|
|
|
|
If there are N subpatterns, there will be N elements in the `submatches` list.
|
|
|
|
Each submatch in this list is a `Maybe` because not all subpatterns may trigger.
|
2014-02-10 19:08:40 +00:00
|
|
|
For example, `(regex "(a+)|(b+)")` will either match many `a`’s or
|
2013-11-01 22:51:23 +00:00
|
|
|
many `b`’s, but never both.
|
|
|
|
* `index` — the index of the match in the original string.
|
|
|
|
* `number` — if you find many matches, you can think of each one
|
|
|
|
as being labeled with a `number` starting at one. So the first time you
|
|
|
|
find a match, that is match `number` one. Second time is match `number` two.
|
2014-02-10 19:08:40 +00:00
|
|
|
This is useful when paired with `replace All` if replacement is dependent on how
|
2013-11-01 22:51:23 +00:00
|
|
|
many times a pattern has appeared before.
|
|
|
|
-}
|
|
|
|
type Match = { match : String, submatches : [Maybe String], index : Int, number : Int }
|
|
|
|
|
2014-02-10 19:08:40 +00:00
|
|
|
{-| `HowMany` is used to specify how many matches you want to make. So
|
|
|
|
`replace All` would replace every match, but `replace (AtMost 2)` would
|
|
|
|
replace at most two matches (i.e. zero, one, two, but never three or more).
|
|
|
|
-}
|
|
|
|
data HowMany = All | AtMost Int
|
|
|
|
|
|
|
|
{-| Find matches in a string:
|
2013-11-01 22:51:23 +00:00
|
|
|
|
2013-11-04 16:06:48 +00:00
|
|
|
```haskell
|
2014-02-10 19:08:40 +00:00
|
|
|
findTwoCommas = find (AtMost 2) (regex ",")
|
2013-11-01 22:51:23 +00:00
|
|
|
|
2014-02-10 19:08:40 +00:00
|
|
|
-- map .index (findTwoCommas "a,b,c,d,e") == [1,3]
|
|
|
|
-- map .index (findTwoCommas "a b c d e") == []
|
2013-11-01 22:51:23 +00:00
|
|
|
|
2014-02-10 19:08:40 +00:00
|
|
|
places = find All (regex "[oi]n a (\\w+)") "I am on a boat in a lake."
|
2013-11-01 22:51:23 +00:00
|
|
|
|
2014-02-10 19:08:40 +00:00
|
|
|
-- map .match places == ["on a boat", "in a lake"]
|
|
|
|
-- map .submatches places == [ [Just "boat"], [Just "lake"] ]
|
2013-11-04 16:06:48 +00:00
|
|
|
```
|
2013-11-01 22:51:23 +00:00
|
|
|
-}
|
2014-02-10 19:08:40 +00:00
|
|
|
find : HowMany -> Regex -> String -> [Match]
|
2013-11-04 16:06:48 +00:00
|
|
|
find = Native.Regex.find
|
2013-11-01 22:51:23 +00:00
|
|
|
|
2014-02-10 19:08:40 +00:00
|
|
|
{-| Replace matches. The function from `Match` to `String` lets
|
2013-11-01 22:51:23 +00:00
|
|
|
you use the details of a specific match when making replacements.
|
|
|
|
|
2013-11-04 16:06:48 +00:00
|
|
|
```haskell
|
2014-02-10 19:08:40 +00:00
|
|
|
devowel = replace All (regex "[aeiou]") (\_ -> "")
|
2013-11-01 22:51:23 +00:00
|
|
|
|
2014-02-10 19:08:40 +00:00
|
|
|
-- devowel "The quick brown fox" == "Th qck brwn fx"
|
2013-11-01 22:51:23 +00:00
|
|
|
|
2014-02-10 19:08:40 +00:00
|
|
|
reverseWords = replace All (regex "\\w+") (\{match} -> String.reverse match)
|
2013-11-01 22:51:23 +00:00
|
|
|
|
2014-02-10 19:08:40 +00:00
|
|
|
-- reverseWords "deliver mined parts" == "reviled denim strap"
|
2013-11-04 16:06:48 +00:00
|
|
|
```
|
2013-11-01 22:51:23 +00:00
|
|
|
-}
|
2014-02-10 19:08:40 +00:00
|
|
|
replace : HowMany -> Regex -> (Match -> String) -> String -> String
|
2013-11-04 16:06:48 +00:00
|
|
|
replace = Native.Regex.replace
|
2013-11-01 22:51:23 +00:00
|
|
|
|
|
|
|
{-| Split a string, using the regex as the separator.
|
|
|
|
|
2013-11-04 16:06:48 +00:00
|
|
|
```haskell
|
2014-02-10 19:08:40 +00:00
|
|
|
split (AtMost 1) (regex ",") "tom,99,90,85" == ["tom","99,90,85"]
|
2013-11-01 22:51:23 +00:00
|
|
|
|
2014-02-10 19:08:40 +00:00
|
|
|
split All (regex ",") "a,b,c,d" == ["a","b","c","d"]
|
2013-11-04 16:06:48 +00:00
|
|
|
```
|
2013-11-01 22:51:23 +00:00
|
|
|
-}
|
2014-02-10 19:08:40 +00:00
|
|
|
split : HowMany -> Regex -> String -> [String]
|
|
|
|
split = Native.Regex.split
|