1-- | A module that exports a simple regex interface. This code is mostly copied
2-- from the regex-compat package at hackage.
3module Text.Hakyll.Regex
10-- | Match a regular expression against a string, returning more information
12matchRegexAll :: Regex -> String -> Maybe (String, String, String, [String])
13matchRegexAll p str = matchM p str
15-- | Replaces every occurance of the given regexp with the replacement string.
16subRegex :: Regex -- ^ Search pattern
17 -> String -- ^ Input string
18 -> String -- ^ Replacement text
19 -> String -- ^ Output string
21subRegex regexp inp replacement =
22 let -- bre matches a backslash then capture either a backslash or some digits
23 bre = makeRegex "\\\\(\\\\|[0-9]+)"
26 lookup' match' repl groups =
27 case matchRegexAll bre repl of
29 Just (lead, _, trail, bgroups) ->
31 if (head bgroups) == "\\"
34 index = (read (head bgroups)) - 1
38 in lead ++ newval ++ lookup' match' trail groups
39 in case matchRegexAll regexp inp of
41 Just (lead, match', trail, groups) ->
42 lead ++ lookup' match' replacement groups ++ (subRegex regexp trail replacement)
44-- | Splits a string based on a regular expression. The regular expression
45-- should identify one delimiter.
46splitRegex :: Regex -> String -> [String]
48splitRegex delim strIn = loop strIn where
49 loop str = case matchOnceText delim str of
51 Just (firstline, _, remainder) ->
54 else firstline : loop remainder
56-- | Split a list at a certain element.
57split :: String -> String -> [String]
58split pattern = filter (not . null)
59 . splitRegex (makeRegex pattern)
61-- | Substitute a regex. Simplified interface.
62substitute :: String -- ^ Pattern to replace (regex).
63 -> String -- ^ Replacement string.
64 -> String -- ^ Input string.
65 -> String -- ^ Result.
66substitute pattern replacement str = subRegex (makeRegex pattern) str replacement
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list