-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.go
43 lines (39 loc) · 986 Bytes
/
regex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"time"
"regexp"
"io/ioutil"
"sort"
"os"
"fmt"
)
func get_date(filename string) string {
reg := regexp.MustCompile(`.* Report \((\d{2}\.\d{2}\.)\-\d{2}\.\d{2}\.\)\.txt`)
a := reg.FindAllStringSubmatch(filename, -1)
if a != nil {
return a[0][1]
} else {
return ""
}
}
func date_from_name(filename string) time.Time {
layout := "02.01."
vreme, _ := time.Parse(layout, get_date(filename))
return vreme
}
func main() {
r := regexp.MustCompile("Nesto")
fajlovi, _ := ioutil.ReadDir("/root/go/src/goregex")
for i := len(fajlovi) - 1; i >= 0; i-- {
if get_date(fajlovi[i].Name()) == "" {
fajlovi = append(fajlovi[:i], fajlovi[i+1:]...)
}
}
sort.Slice(fajlovi, func(i, j int) bool {
return date_from_name(fajlovi[i].Name()).Before(date_from_name(fajlovi[j].Name()))
})
for _, i := range fajlovi {
fmt.Println(i.Name(), "==>", r.ReplaceAllString(i.Name(), "Primer"))
os.Rename(i.Name(), r.ReplaceAllString(i.Name(), "Primer"))
}
}