@@ -0,0 +1,128 @@
2+{-| A library for working with regular expressions. It uses [the
3+same kind of regular expressions accepted by JavaScript](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions).
6+@docs pattern, caseInsensitive, escape
12+@docs contains, find, findN
15+@docs replace, replaceN
27+{-| Escape all special characters. So `pattern (escape "$$$")`
28+will match exactly `"$$$"` even though `$` is a special character.
30+escape : String -> String
31+escape = Native.Regex.escape
33+{-| 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).
34+Be careful to escape backslashes properly!
36+pattern : String -> Regex
37+pattern = Native.Regex.pattern
40+{-| Make a pattern case insensitive -}
41+caseInsensitive : Regex -> Regex
42+caseInsensitive = Native.Regex.caseInsensitive
44+{-| Check to see if a Regex is contained in a string.
46+ contains (pattern "123") "12345" == True
47+ contains (pattern "b+") "aabbcc" == True
49+ contains (pattern "789") "12345" == False
50+ contains (pattern "z+") "aabbcc" == False
52+contains : Regex -> String -> Bool
53+contains = Native.Regex.contains
55+{-| A `Match` represents all of the details about a particular match in a string.
56+Here are details on each field:
58+ * `match` — the full string of the match.
59+ * `submatches` — a pattern might have [subpatterns, surrounded by
60+ parentheses](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Parenthesized_Substring_Matches).
61+ If there are N subpatterns, there will be N elements in the `submatches` list.
62+ Each submatch in this list is a `Maybe` because not all subpatterns may trigger.
63+ For example, `(pattern "(a+)|(b+)")` will either match many `a`’s or
64+ many `b`’s, but never both.
65+ * `index` — the index of the match in the original string.
66+ * `number` — if you find many matches, you can think of each one
67+ as being labeled with a `number` starting at one. So the first time you
68+ find a match, that is match `number` one. Second time is match `number` two.
69+ This is useful when paired with `replace` if replacement is dependent on how
70+ many times a pattern has appeared before.
72+type Match = { match : String, submatches : [Maybe String], index : Int, number : Int }
74+{-| Find all of the matches in a string:
76+ words = find (pattern "\\w+") "hello world"
78+ map .match words == ["hello","world"]
79+ map .index words == [0,6]
81+ places = find (pattern "[oi]n a (\\w+)") "I am on a boat in a lake."
83+ map .match places == ["on a boat", "in a lake"]
84+ map .submatches places == [ [Just "boat"], [Just "lake"] ]
86+find : Regex -> String -> [Match]
87+find = Native.Regex.find
89+{-| Same as `find`, but `findN` will quit searching after the Nth match.
90+That means the resulting list has maximum length N, but *it can be shorter*
91+if there are not that many matches in the given string.
93+findN : Int -> Regex -> String -> [Match]
94+findN = Native.Regex.findN
96+{-| Replace all matches. The function from `Match` to `String` lets
97+you use the details of a specific match when making replacements.
99+ vowels = caseInsensitive (pattern "[aeiou]")
100+ devowel = replace vowels (\_ -> "")
102+ devowel "The quick brown fox" == "Th qck brwn fx"
104+ word = pattern "\\w+"
105+ reverseWords = replace word (\{match} -> String.reverse match)
107+ reverseWords "deliver mined parts" == "reviled denim strap"
109+replace : Regex -> (Match -> String) -> String -> String
110+replace = Native.Regex.replace
112+{-| Same as `replace`, but `replaceN` will quit after the Nth match.-}
113+replaceN : Int -> Regex -> (Match -> String) -> String -> String
114+replaceN = Native.Regex.replaceN
116+{-| Split a string, using the regex as the separator.
118+ split (pattern ", ?") "a,b, c, d" == ["a","b","c","d"]
120+split : Regex -> String -> [String]
121+split = Native.Regex.split
123+{-| Same as `split` but stops after the Nth match.
125+ splitN 1 (pattern ", ?") "a,b,c,d" == ["a","b,c,d"]
127+splitN : Int -> Regex -> String -> [String]
128+splitN = Native.Regex.splitN