Skip to content

Commit

Permalink
Exclude regex special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrhmkoz committed Sep 29, 2023
1 parent 171569a commit e1ff3f2
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 29 deletions.
11 changes: 11 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 49 additions & 29 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import (
)

func Parse(format, target string) ([]map[string]interface{}, error) {
// Temporarily replace escaped braces with placeholder strings
format = strings.ReplaceAll(format, `\\{`, `{{OPEN_BRACE}}`)
format = strings.ReplaceAll(format, `\\}`, `{{CLOSE_BRACE}}`)

// Escape regex special characters, excluding '{' and '}'
var specialCharacters = []string{`.`, `^`, `$`, `*`, `+`, `?`, `(`, `)`, `[`, `]`, `|`}
for _, char := range specialCharacters {
format = strings.ReplaceAll(format, char, "\\"+char)
}

// Process custom tags as before
format = regexp.MustCompile(`{:s:(\w+)}`).ReplaceAllString(format, `(?P<${1}_s>.+)`)
format = regexp.MustCompile(`{:d:(\w+)}`).ReplaceAllString(format, `(?P<${1}_d>\d+)`)
format = regexp.MustCompile(`{:f:(\w+)}`).ReplaceAllString(format, `(?P<${1}_f>\d+(\.\d+)?)`)
Expand All @@ -17,6 +28,10 @@ func Parse(format, target string) ([]map[string]interface{}, error) {
format = regexp.MustCompile(`{:i}`).ReplaceAllString(format, `(?:.|\n)*?`)
format = regexp.MustCompile(`{:e}`).ReplaceAllString(format, `\s*?`)

// Revert the temporary placeholders back to their original form
format = strings.ReplaceAll(format, `{{OPEN_BRACE}}`, `\{`)
format = strings.ReplaceAll(format, `{{CLOSE_BRACE}}`, `\}`)

re := regexp.MustCompile(format)
matches := re.FindAllStringSubmatch(target, -1)

Expand All @@ -30,39 +45,44 @@ func Parse(format, target string) ([]map[string]interface{}, error) {
for i, name := range re.SubexpNames() {
if i != 0 && name != "" {
value := match[i]
if strings.HasSuffix(name, "_d") {
name = name[:len(name)-2]
result[name], _ = strconv.Atoi(value)
} else if strings.HasSuffix(name, "_f") {
name = name[:len(name)-2]
result[name], _ = strconv.ParseFloat(value, 64)
} else if strings.HasSuffix(name, "_ad") {
name = name[:len(name)-3]
split := strings.Split(value, ", ")
intArray := make([]int, len(split))
for i, s := range split {
intArray[i], _ = strconv.Atoi(s)
}
result[name] = intArray
} else if strings.HasSuffix(name, "_af") {
name = name[:len(name)-3]
split := strings.Split(value, ", ")
floatArray := make([]float64, len(split))
for i, s := range split {
floatArray[i], _ = strconv.ParseFloat(s, 64)
}
result[name] = floatArray
} else if strings.HasSuffix(name, "_a") {
name = name[:len(name)-2]
result[name] = strings.Split(value, ", ")
} else {
name = name[:len(name)-2]
result[name] = value
}
processParsedValue(value, name, result)
}
}
results = append(results, result)
}

return results, nil
}

// Rest of the code remains the same...
func processParsedValue(value, name string, result map[string]interface{}) {
if strings.HasSuffix(name, "_d") {
name = name[:len(name)-2]
result[name], _ = strconv.Atoi(value)
} else if strings.HasSuffix(name, "_f") {
name = name[:len(name)-2]
result[name], _ = strconv.ParseFloat(value, 64)
} else if strings.HasSuffix(name, "_ad") {
name = name[:len(name)-3]
split := strings.Split(value, ", ")
intArray := make([]int, len(split))
for i, s := range split {
intArray[i], _ = strconv.Atoi(s)
}
result[name] = intArray
} else if strings.HasSuffix(name, "_af") {
name = name[:len(name)-3]
split := strings.Split(value, ", ")
floatArray := make([]float64, len(split))
for i, s := range split {
floatArray[i], _ = strconv.ParseFloat(s, 64)
}
result[name] = floatArray
} else if strings.HasSuffix(name, "_a") {
name = name[:len(name)-2]
result[name] = strings.Split(value, ", ")
} else {
name = name[:len(name)-2]
result[name] = value
}
}
36 changes: 36 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,39 @@ func TestParseOnTestLog(t *testing.T) {
t.Errorf("expected %v, got %v", expected, result)
}
}

func TestParseWithSpecialCharacters(t *testing.T) {
format := `
\{:s:Name\}
.*^Name: {:s:Name}$
*+Surname: {:s:Surname}?()[]|
Age: {:d:Age}
Colors: {:a:Colors}`

target := `
{:s:Name}
.*^Name: John$
*+Surname: Wayne?()[]|
Age: 30
Colors: red, blue, green`

expected := map[string]interface{}{
"Name": "John",
"Surname": "Wayne",
"Age": 30,
"Colors": []string{"red", "blue", "green"},
}

results, err := Parse(format, target)
if err != nil {
t.Errorf("expected no error, got %v", err)
}

if len(results) == 0 {
t.Fatal("No results found.")
}

if !reflect.DeepEqual(results[0], expected) {
t.Errorf("expected %v, got %v", expected, results[0])
}
}

0 comments on commit e1ff3f2

Please sign in to comment.