How to replace multiple consecutive question marks (?) with a single question mark in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "regexp"
)

func main() {
    str := "What??? Why?? How?????"

    // Compile the regex to match one or more '?'
    re := regexp.MustCompile(`\?+`)
    
    // Replace matches with a single '?'
    result := re.ReplaceAllString(str, "?")

    fmt.Println(result)
}



/*
run:

What? Why? How?

*/

 



answered Jul 24, 2025 by avibootz
...