diff options
| author | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
|---|---|---|
| committer | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
| commit | 37da2899f40661e3e9631e497da8dc59b971cbd0 (patch) | |
| tree | cbc6d4680e347d906f5fa7fca73214418741df72 /appl/lib/regexutils.b | |
| parent | 54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff) | |
20060303a
Diffstat (limited to 'appl/lib/regexutils.b')
| -rw-r--r-- | appl/lib/regexutils.b | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/appl/lib/regexutils.b b/appl/lib/regexutils.b new file mode 100644 index 00000000..4040f0bf --- /dev/null +++ b/appl/lib/regexutils.b @@ -0,0 +1,65 @@ +implement RegexUtils; + +# matching and substitution functions +# evb@lucent.com + +include "sys.m"; + sys: Sys; + +include "regexutils.m"; + +init() +{ + if (sys == nil) + sys = load Sys Sys->PATH; + + regex = load Regex Regex->PATH; + if (regex == nil) + raise "fail: Regex not loaded"; +} + +match(pattern: Regex->Re, s: string): string +{ + pos := regex->execute(pattern, s); + if (pos == nil) + return ""; + (beg, end) := pos[0]; + + return s[beg:end]; +} + +match_mult(pattern: Regex->Re, s: string): array of (int, int) +{ + return regex->execute(pattern, s); +} + +sub(text, pattern, new: string): string +{ + return sub_re(text, regex->compile(pattern, 0).t0, new); +} + +sub_re(text: string, pattern: Regex->Re, new: string): string +{ + pos := regex->execute(pattern, text); + if (pos == nil) + return text; + + (beg, end) := pos[0]; + newline := text[:beg] + new + text[end:]; + return newline; +} + +subg(text, pattern, new: string): string +{ + return subg_re(text, regex->compile(pattern, 0).t0, new); +} + +subg_re(text: string, pattern: Regex->Re, new: string): string +{ + oldtext := text; + while ( (text = sub_re(text, pattern, new)) != oldtext) { + oldtext = text; + } + + return text; +} |
