How to check if a string includes $sometext$ without numbers in Go

1 Answer

0 votes
package main

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

func includeDollarSymbolText(text string) bool {
    // Regex to match $word$, case-insensitive
    re := regexp.MustCompile(`(?i)\$[a-z]+\$`)
    cleanedText := re.ReplaceAllString(text, "")

    // Check if any $ remains
    return !strings.Contains(cleanedText, "$")
}

func main() {
    fmt.Println(includeDollarSymbolText("abc xy $text$ z"))            // ok
    fmt.Println(includeDollarSymbolText("abc xy $ text$ z"))           // space
    fmt.Println(includeDollarSymbolText("abc xy $$ z"))                // empty
    fmt.Println(includeDollarSymbolText("abc 100 $text$ z"))           // ok
    fmt.Println(includeDollarSymbolText("abc $1000 $text$ z"))         // open $
    fmt.Println(includeDollarSymbolText("abc xy $IBM$ z $Microsoft$")) // ok
    fmt.Println(includeDollarSymbolText("abc xy $F3$ z"))              // include number
    fmt.Println(includeDollarSymbolText("abc xy $text z"))             // missing close $
}



/*
run:

true
false
false
true
false
true
false
false

*/




 



answered Jul 11, 2025 by avibootz
...