summaryrefslogtreecommitdiff
path: root/re.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-01-01 16:47:42 +0100
committerDimitri Sokolyuk <demon@dim13.org>2018-01-01 16:47:42 +0100
commitb805a548e3ead263010cbf03717aa9ac52ab301d (patch)
tree76bee1feb9fb3d40ae7f65c2811752285bb28b32 /re.go
parent92c01374aa44e5800ce19a294170ad317489b439 (diff)
readd regexp fixer
Diffstat (limited to 're.go')
-rw-r--r--re.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/re.go b/re.go
new file mode 100644
index 0000000..341488c
--- /dev/null
+++ b/re.go
@@ -0,0 +1,33 @@
+package main
+
+import (
+ "regexp"
+ "strings"
+)
+
+func re(s, r string, global bool) string {
+ // min: at least two separators
+ if len(s) < 2 {
+ return ""
+ }
+ z := strings.Split(r[1:], string(r[0]))
+ // match // and ///
+ if len(z) < 2 || len(z) > 3 {
+ return ""
+ }
+ re, err := regexp.Compile(z[0])
+ if err != nil {
+ return ""
+ }
+ i := 1
+ if global {
+ i = -1
+ }
+ return re.ReplaceAllStringFunc(s, func(b string) string {
+ if i != 0 {
+ i -= 1
+ return z[1]
+ }
+ return b
+ })
+}