@@ -3,16 +3,17 @@ module Regex where
3same kind of regular expressions accepted by JavaScript](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions).
3same kind of regular expressions accepted by JavaScript](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions).
6−@docs pattern, caseInsensitive, escape
6+@docs regex, escape, caseInsensitive
8−# Match
8+# Helpful Data Structures
11−# Find and Replace
10+These data structures are needed to help define functions like [`find`](#find)
12−@docs contains, find, findAll, replace, replaceAll
11+and [`replace`](#replace).
14−# Split
13+@docs HowMany, Match
15−@docs split, splitN
16+@docs contains, find, replace, split
@@ -21,31 +22,35 @@ import Native.Regex
22data Regex = Regex
23data Regex = Regex
24−{-| Escape all special characters. So `pattern (escape "$$$")`
25+{-| Escape strings to be regular expressions, making all special characters
25−will match exactly `"$$$"` even though `$` is a special character.
26+safe. So `regex (escape "^a+")` will match exactly `"^a+"` instead of a series
27+of `a`’s that start at the beginning of the line.
27escape : String -> String
29escape : String -> String
28escape = Native.Regex.escape
30escape = Native.Regex.escape
30{-| 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).
32{-| 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).
31−Be careful to escape backslashes properly!
34+Be careful to escape backslashes properly! For example, `"\w"` is escaping the
35+letter `w` which is probably not what you want. You probably want `"\\w"`
36+instead, which escapes the backslash.
33−pattern : String -> Regex
38+regex : String -> Regex
34−pattern = Native.Regex.pattern
39+regex = Native.Regex.regex
37−{-| Make a pattern case insensitive -}
42+{-| Make a regex case insensitive -}
38caseInsensitive : Regex -> Regex
43caseInsensitive : Regex -> Regex
39caseInsensitive = Native.Regex.caseInsensitive
44caseInsensitive = Native.Regex.caseInsensitive
41{-| Check to see if a Regex is contained in a string.
46{-| Check to see if a Regex is contained in a string.
44− contains (pattern "123") "12345" == True
49+ contains (regex "123") "12345" == True
45− contains (pattern "b+") "aabbcc" == True
50+ contains (regex "b+") "aabbcc" == True
47− contains (pattern "789") "12345" == False
52+ contains (regex "789") "12345" == False
48− contains (pattern "z+") "aabbcc" == False
53+ contains (regex "z+") "aabbcc" == False
51contains : Regex -> String -> Bool
56contains : Regex -> String -> Bool
@@ -55,79 +60,67 @@ contains = Native.Regex.contains
55Here are details on each field:
60Here are details on each field:
57 * `match` — the full string of the match.
62 * `match` — the full string of the match.
58− * `submatches` — a pattern might have [subpatterns, surrounded by
63+ * `submatches` — a regex might have [subpatterns, surrounded by
59 parentheses](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Parenthesized_Substring_Matches).
64 parentheses](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Parenthesized_Substring_Matches).
60 If there are N subpatterns, there will be N elements in the `submatches` list.
65 If there are N subpatterns, there will be N elements in the `submatches` list.
61 Each submatch in this list is a `Maybe` because not all subpatterns may trigger.
66 Each submatch in this list is a `Maybe` because not all subpatterns may trigger.
62− For example, `(pattern "(a+)|(b+)")` will either match many `a`’s or
67+ For example, `(regex "(a+)|(b+)")` will either match many `a`’s or
63 many `b`’s, but never both.
68 many `b`’s, but never both.
64 * `index` — the index of the match in the original string.
69 * `index` — the index of the match in the original string.
65 * `number` — if you find many matches, you can think of each one
70 * `number` — if you find many matches, you can think of each one
66 as being labeled with a `number` starting at one. So the first time you
71 as being labeled with a `number` starting at one. So the first time you
67 find a match, that is match `number` one. Second time is match `number` two.
72 find a match, that is match `number` one. Second time is match `number` two.
68− This is useful when paired with `replaceAll` if replacement is dependent on how
73+ This is useful when paired with `replace All` if replacement is dependent on how
69 many times a pattern has appeared before.
74 many times a pattern has appeared before.
71type Match = { match : String, submatches : [Maybe String], index : Int, number : Int }
76type Match = { match : String, submatches : [Maybe String], index : Int, number : Int }
78+{-| `HowMany` is used to specify how many matches you want to make. So
79+`replace All` would replace every match, but `replace (AtMost 2)` would
80+replace at most two matches (i.e. zero, one, two, but never three or more).
82+data HowMany = All | AtMost Int
73−{-| Find all of the matches in a string:
84+{-| Find matches in a string:
76− words = findAll (pattern "\\w+") "hello world"
87+ findTwoCommas = find (AtMost 2) (regex ",")
78− map .match words == ["hello","world"]
89+ -- map .index (findTwoCommas "a,b,c,d,e") == [1,3]
79− map .index words == [0,6]
90+ -- map .index (findTwoCommas "a b c d e") == []
81− places = findAll (pattern "[oi]n a (\\w+)") "I am on a boat in a lake."
92+ places = find All (regex "[oi]n a (\\w+)") "I am on a boat in a lake."
83− map .match places== ["on a boat", "in a lake"]
94+ -- map .match places == ["on a boat", "in a lake"]
84− map .submatches places == [ [Just "boat"], [Just "lake"] ]
95+ -- map .submatches places == [ [Just "boat"], [Just "lake"] ]
87−findAll : Regex -> String -> [Match]
88−findAll = Native.Regex.findAll
90−{-| Same as `findAll`, but `find` will quit searching after the *n<sup>th</sup>* match.
91−That means the resulting list has maximum length N, but *it can be shorter*
92−if there are not that many matches in the given string.
94−find : Int -> Regex -> String -> [Match]
98+find : HowMany -> Regex -> String -> [Match]
95find = Native.Regex.find
99find = Native.Regex.find
97−{-| Replace all matches. The function from `Match` to `String` lets
101+{-| Replace matches. The function from `Match` to `String` lets
98you use the details of a specific match when making replacements.
102you use the details of a specific match when making replacements.
100```haskell
104```haskell
101− devowel = replaceAll (pattern "[aeiou]") (\_ -> "")
105+ devowel = replace All (regex "[aeiou]") (\_ -> "")
103− devowel "The quick brown fox" == "Th qck brwn fx"
107+ -- devowel "The quick brown fox" == "Th qck brwn fx"
105− reverseWords = replaceAll (pattern "\\w+") (\{match} -> String.reverse match)
109+ reverseWords = replace All (regex "\\w+") (\{match} -> String.reverse match)
107− reverseWords "deliver mined parts" == "reviled denim strap"
111+ -- reverseWords "deliver mined parts" == "reviled denim strap"
110−replaceAll : Regex -> (Match -> String) -> String -> String
111−replaceAll = Native.Regex.replaceAll
113−{-| Same as `replaceAll`, but `replace` will quit after the *n<sup>th</sup>* match.-}
114−replace : Int -> Regex -> (Match -> String) -> String -> String
114+replace : HowMany -> Regex -> (Match -> String) -> String -> String
115replace = Native.Regex.replace
115replace = Native.Regex.replace
117{-| Split a string, using the regex as the separator.
117{-| Split a string, using the regex as the separator.
119```haskell
119```haskell
120− split (pattern " *, *") "a ,b, c,d" == ["a","b","c","d"]
120+ split (AtMost 1) (regex ",") "tom,99,90,85" == ["tom","99,90,85"]
123−split : Regex -> String -> [String]
124−split = Native.Regex.split
126−{-| Same as `split` but stops after the *n<sup>th</sup>* match.
122+ split All (regex ",") "a,b,c,d" == ["a","b","c","d"]
129− splitN 1 (pattern ": *") "tom: 99,90,85" == ["tom","99,90,85"]
132−splitN : Int -> Regex -> String -> [String]
125+split : HowMany -> Regex -> String -> [String]
133−splitN = Native.Regex.splitN
126+split = Native.Regex.split