diff options
author | Dimitri Sokolyuk <demon@dim13.org> | 2018-01-01 16:47:42 +0100 |
---|---|---|
committer | Dimitri Sokolyuk <demon@dim13.org> | 2018-01-01 16:47:42 +0100 |
commit | b805a548e3ead263010cbf03717aa9ac52ab301d (patch) | |
tree | 76bee1feb9fb3d40ae7f65c2811752285bb28b32 /re.go | |
parent | 92c01374aa44e5800ce19a294170ad317489b439 (diff) |
readd regexp fixer
Diffstat (limited to 're.go')
-rw-r--r-- | re.go | 33 |
1 files changed, 33 insertions, 0 deletions
@@ -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 + }) +} |