Last active
August 29, 2015 14:13
-
-
Save oprypin/15ed8ea0bc3f35e973fc to your computer and use it in GitHub Desktop.
Regular expression replacement using results of calling a procedure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 6ae230f9c9809141deea110900a36242ac9256dc Mon Sep 17 00:00:00 2001 | |
From: Oleh Prypin <[email protected]> | |
Date: Thu, 8 Jan 2015 00:09:07 +0200 | |
Subject: [PATCH] Add regex replacement using results of calling a procedure | |
--- | |
lib/impure/re.nim | 29 +++++++++++++++++++++++++++++ | |
1 file changed, 29 insertions(+) | |
diff --git a/lib/impure/re.nim b/lib/impure/re.nim | |
index a7bebb8..be57c8f 100644 | |
--- a/lib/impure/re.nim | |
+++ b/lib/impure/re.nim | |
@@ -327,6 +327,35 @@ proc replacef*(s: string, sub: Regex, by: string): string = | |
prev = match.last + 1 | |
add(result, substr(s, prev)) | |
+proc replace*(s: string, sub: Regex, by: proc(s: string): string): string = | |
+ ## Replaces `sub` in `s` by the results of calling the proc `by` with | |
+ ## each matched string. | |
+ result = "" | |
+ var prev = 0 | |
+ while true: | |
+ var match = findBounds(s, sub, prev) | |
+ if match.first < 0: break | |
+ add(result, substr(s, prev, match.first-1)) | |
+ add(result, by(substr(s, match.first, match.last))) | |
+ prev = match.last + 1 | |
+ add(result, substr(s, prev)) | |
+ | |
+proc replacef*(s: string, sub: Regex, | |
+ by: proc(m: openArray[string]): string): string = | |
+ ## Replaces `sub` in `s` by the results of calling the proc `by` with | |
+ ## each array of captures. | |
+ result = "" | |
+ var caps: array[0..MaxSubpatterns-1, string] | |
+ var prev = 0 | |
+ while true: | |
+ var match = findBounds(s, sub, caps, prev) | |
+ if match.first < 0: break | |
+ add(result, substr(s, prev, match.first-1)) | |
+ let capCount = find(caps, string(nil)) | |
+ add(result, by(caps[0..capCount-1])) | |
+ prev = match.last + 1 | |
+ add(result, substr(s, prev)) | |
+ | |
proc parallelReplace*(s: string, subs: openArray[ | |
tuple[pattern: Regex, repl: string]]): string = | |
## Returns a modified copy of `s` with the substitutions in `subs` | |
-- | |
2.2.1 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re, strutils | |
proc replace*(s: string, sub: Regex, by: proc(s: string): string): string = | |
## Replaces `sub` in `s` by the results of calling the proc `by` with | |
## each matched string. | |
result = "" | |
var prev = 0 | |
while true: | |
var match = findBounds(s, sub, prev) | |
if match.first < 0: break | |
add(result, substr(s, prev, match.first-1)) | |
add(result, by(substr(s, match.first, match.last))) | |
prev = match.last + 1 | |
add(result, substr(s, prev)) | |
echo "particles are particularly interesting".replace(re"part", toUpper) | |
# PARTicles are PARTicularly interesting | |
proc replacef*(s: string, sub: Regex, | |
by: proc(m: openArray[string]): string): string = | |
## Replaces `sub` in `s` by the results of calling the proc `by` with | |
## each array of captures. | |
result = "" | |
var caps: array[0..MaxSubpatterns-1, string] | |
var prev = 0 | |
while true: | |
let match = findBounds(s, sub, caps, prev) | |
if match.first < 0: break | |
add(result, substr(s, prev, match.first-1)) | |
let capCount = find(caps, string(nil)) | |
add(result, by(caps[0..capCount-1])) | |
prev = match.last + 1 | |
add(result, substr(s, prev)) | |
echo "RUMPF Andreas, DUMEZ Cristophe".replacef( | |
re"\b([A-Z]{3,})\ ([A-Z][a-z]{2,})\b") do (m: openarray[string]) -> string: | |
"$2 $1" % m.map(toLower).map(capitalize) | |
# Andreas Rumpf, Cristophe Dumez |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment