-2

my go script should add one newline before matching the regEx-Search-String ^(.+[,]+\n). The Prototype i had tested before into the editor: enter image description here i want add newlines before this lines: \n$1. This works if i try it into the Text-Editor.

If i try this (see line 24) with my script it is changing nothing and sends no error. Any ideas what i do wrong?

Example

i like to use PCRE like it works in this Example https://regex101.com/r/sB9wW6/17

Same Example here:

Example source

Dear sir,
Thanks for your interest.

expected result

#### here is a newline ####
Dear sir,
Thanks for your interest.

result is (produced by the script below)

Dear sir,
Thanks for your interest.

go script:

// replace in files and store the new copy of it.
package main
import (
    "fmt"
    "io/ioutil"
    "os"
    "path/filepath"
    "regexp"
    "strings"
    "time"
)
func visit(path string, fi os.FileInfo, err error) error {
    matched, err := filepath.Match("*.csv", fi.Name())
    if err != nil {
        panic(err)
        return err
    }
    if matched {
        read, err := ioutil.ReadFile(path)
        if err != nil {
            panic(err)
        }
        newContents := string(read)
        newContents = regExRepl(`^(.+[,]+\n)`, newContents, `\n\n\n$1`)

        var re = regexp.MustCompile(`[\W]+`)
        t_yymmdd := regexp.MustCompile(`[\W]+`).ReplaceAllString(time.Now().Format(time.RFC3339), `-`)[:10]
        t_hhss := re.ReplaceAllString(time.Now().Format(time.RFC3339), `-`)[11:19]
        t_yymmddhhss := t_yymmdd + "_" + t_hhss
        fmt.Println(t_yymmddhhss)

        filePath := fileNameWithoutExtension(path) + t_yymmddhhss + ".csv"
        err = ioutil.WriteFile(filePath, []byte(newContents), 0)
        if err != nil {
            panic(err)
        }
    }
    return nil
}

func regExRepl(regExPatt string, newContents string, regExRepl string) string {
    return regexp.MustCompile(regExPatt).ReplaceAllString(newContents, regExRepl)
}
func main() {
    err := filepath.Walk("./november2020messages.csv", visit) // <== read all files in current folder 20:12:06 22:44:42
    if err != nil {
        panic(err)
    }
}
func fileNameWithoutExtension(fileName string) string {
    return strings.TrimSuffix(fileName, filepath.Ext(fileName))
}
SL5net
  • 2,282
  • 4
  • 28
  • 44
  • 2
    Please see the comments to [this question](https://stackoverflow.com/q/64934802/720999) and the Q&A [the reason](https://stackoverflow.com/questions/39636124/) for its closure refers to. – kostix Dec 07 '20 at 12:06
  • Btw: The while idea of regexp.MustCompile is that you do it once, upfront. Not everytime you need the same regexp. – Volker Dec 07 '20 at 12:12
  • @Volker i use this regex not often. in my case there are only one or sometimes two files in the source folder. – SL5net Dec 07 '20 at 13:07
  • @kstix i like to use PCRE like it works here https://regex101.com/r/sB9wW6/16 – SL5net Dec 07 '20 at 13:07
  • 1
    it is a waste to use regexp on such requirement. –  Dec 07 '20 at 13:09
  • @mh-cbon i have to use about twenty regex in my case. not only one. this is just a simplification. i like to use then the same technic with all – SL5net Dec 07 '20 at 13:12
  • 1
    please update your demo data and expected output because as of now, i would just recommend you to prepend as many `\n` as you need.. –  Dec 07 '20 at 13:23
  • @mh-cbon yes now i use only one `\n` – SL5net Dec 07 '20 at 13:31
  • [35 35 35 32 119 101 32 107 110 111 119 32 119 104 97 116 32 97 32 110 101 119 32 108 105 110 101 32 105 115 32 35 35 35 35] –  Dec 07 '20 at 13:36

1 Answers1

0
  • for interpretation \n as newline don't us

    `\n`` use "\n"

  • may use ^(.+[,]+) instead ^(.+[,]+\n) and ad (?m) before for multi-line replacements

this suggestion you could test here: https://play.golang.org/p/25_0GJ93oCT

The following example illustrates the difference (in golang-playground here https://play.golang.org/p/FkPwElhx-Xu ):

// example from:
package main
import (
    "fmt"
    "regexp"
)

func main() {
    newContents := `line 1,
line 2
line a,
line b`
    newContents1 := regexp.MustCompile(`^(.+[,]+\n)`).ReplaceAllString(newContents, `\n$1`)
    fmt.Println("hi\n" + newContents1)
    newContents1 = regexp.MustCompile(`(?m)^(.+[,]+\n)`).ReplaceAllString(newContents, "\n$1")
    fmt.Println("ho\n" + newContents1)
}

Result:

hi
\nline 1,
line 2
line a,
line b
ho

line 1,
line 2

line a,
line b
SL5net
  • 2,282
  • 4
  • 28
  • 44