Go RE2 Syntax for filtering out substrings that contain any number

77 views Asked by At

I currently have a functioning Go RE2 regex pattern that trims a given string by removing everything after the second dash. For example, bob-marley-bla-bla is transformed into bob-marley.

[^-]+-[^-]+ Now, I need to enhance this regex to remove all substrings that have any numeric digit in it (e.g 67srer) except when the substring is "s3".

For instance:

bob-7878dccdf-marley => bob-marley 
8sdfhsdf-bob-marley => bob-marley 
s3-bob-marley => s3-bob-marley 
bob-666777-marley => bob-marley
s3-sd8s7dfx-bob-marley-9d8f9 => s3-bob-marley
hgfs889899-bob-marley => bob-marley 
bob-45345sdfsfd-marley => bob-marley 
bob-marley-8s7d8f7s8df => bob-marley

Context: I need to use this regex in configuring Promtail's regex stage.

Below is my current code that I want to be fixed to incorporate the aforementioned requirement:

package main

import (
    "fmt"
    "regexp"
    "strings"
)

func main() {
    // Define the regular expression pattern to match sequences of characters that are not 's3' or '-'
    pattern := regexp.MustCompile(`[^-]+-[^-]+`)

    // Test cases
    testCases := []string{
        "bob-7878dccdf-marley",
        "bob-666777-marley",
        "8sdfhsdf-bob-marley",
        "s3-bob-marley",
        "s3-sd8s7dfx-bob-marley-9d8f9",
        "hgfs889899-bob-marley",
        "bob-45345sdfsfd-marley",
        "bob-marley-8s7d8f7s8df",
    }

    for _, testCase := range testCases {
        // Remove unwanted characters
        result := pattern.FindAllString(testCase, -1)
        // Join the matched sequences
        resultStr := strings.Join(result, "")
        fmt.Printf("%s is changed to %s\n", testCase, resultStr)
    }
}

Any assistance in adjusting the regex pattern to achieve the desired transformation would be greatly appreciated. Thank you!

I tried [^\w\-s3] but it did not help.

1

There are 1 answers

1
Alex Pliutau On
package main

import (
    "fmt"
    "regexp"
)

func main() {
    // Define the regular expression pattern
    pattern := regexp.MustCompile(`(?<!s3)-([^-]*)(?!s3)|s3`)

    // Test cases
    testCases := []string{
        "bob-7878dccdf-marley",
        "bob-666777-marley",
        "8sdfhsdf-bob-marley",
        "s3-bob-marley",
        "s3-sd8s7dfx-bob-marley-9d8f9",
        "hgfs889899-bob-marley",
        "bob-45345sdfsfd-marley",
        "bob-marley-8s7d8f7s8df",
    }

    for _, testCase := range testCases {
        // Replace all matches with empty string
        result := pattern.ReplaceAllString(testCase, "")
        fmt.Printf("%s is changed to %s\n", testCase, result)
    }
}