1 import "Prelude" hiding (group)
3 importJava "java.util.regex.Pattern" where
6 compile :: String -> Pattern
7 matcher :: Pattern -> String -> Matcher
9 importJava "java.util.regex.Matcher" where
12 matches :: Matcher -> <Proc> Boolean
14 group :: Matcher -> <Proc> String
17 matcherFind :: Matcher -> <Proc> Boolean
19 appendReplacement :: Matcher -> StringBuffer -> String -> <Proc> Matcher
20 appendTail :: Matcher -> StringBuffer -> <Proc> StringBuffer
23 replaceAll_ :: Matcher -> String -> String
25 @JavaName replaceFirst
26 replaceFirst_ :: Matcher -> String -> String
28 quoteReplacement :: String -> String
30 importJava "java.lang.StringBuffer" where
34 newStringBuffer :: () -> StringBuffer
37 freezeStringBuffer :: StringBuffer -> <Proc> String
40 @replaceAll pattern replacement text@ replaces all matches of @pattern@ in the @text@
43 replaceAll :: String -> String -> String -> String
44 replaceAll pattern replacement text =
45 replaceAll_ (matcher (compile pattern) text) replacement
48 @replaceFirst pattern replacement text@ replaces the first match of @pattern@ in the @text@
51 replaceFirst :: String -> String -> String -> String
52 replaceFirst pattern replacement text =
53 replaceFirst_ (matcher (compile pattern) text) replacement
56 @substituteAll pattern f text@ replaces all matches of @pattern@ in the @text@
57 by the string returned by @f@ given the matched region of the string as a parameter.
59 substituteAll :: String -> (String -> String) -> String -> String
60 substituteAll pattern f text = runProc do
61 m = matcher (compile pattern) text
62 buf = newStringBuffer ()
63 while (matcherFind m) do
64 appendReplacement m buf $ f $ group m
66 freezeStringBuffer buf
70 @substituteFirst pattern f text@ replaces the first match of @pattern@ in the @text@
71 by the string returned by @f@ given the matched region of the string as a parameter.
73 substituteFirst :: String -> (String -> String) -> String -> String
74 substituteFirst pattern f text = runProc do
75 m = matcher (compile pattern) text
76 if (matcherFind m) then do
77 buf = newStringBuffer ()
78 appendReplacement m buf $ f $ group m
80 freezeStringBuffer buf