summaryrefslogtreecommitdiff
path: root/re.go
diff options
context:
space:
mode:
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
+ })
+}