punctuation mark in Golang

148 Views Asked by At

I'm working on a Golang text editor but i have an issue when solving for punctuation the requirement is as follow.

The punctuation mark ' will always be found with another instance of it and they should be placed to the right and left of the word in the middle of them, without any spaces. (Ex: "I am exactly how they describe me: ' awesome '" -> "I am exactly how they describe me: 'awesome'")

If there are more than one word between the two ' ' marks, the program should place the marks next to the corresponding words (Ex: "As Elton John said: ' I am the most well-known homosexual in the world '" -> "As Elton John said: 'I am the most well-known homosexual in the world'")

but when i add an extra ' that part of word it's ignore the last '.

For example after excuting the code:

 - input: John say's: ' may name is john and I'm Good ' 
 - currents output: John say's: 'may name is john and I'm Good ' -> **wrong**
 - The true output must be: John say's: 'may name is john and I'm Good' 

as the ' that is on i'm is part of a word.

the code is

func FormatString(input string) string {
re := regexp.MustCompile(`'([^']*)'`)
formattedInput := re.ReplaceAllStringFunc(input, func(matched string) string {
    // Remove leading/trailing spaces inside the quotation marks
    innerText := matched[1 : len(matched)-1]
    // Split the inner text into words and trim spaces
    words := strings.Fields(innerText)
    // Reconstruct the formatted substring with proper spacing
    formattedWords := strings.Join(words, " ")
    return fmt.Sprintf("'%s'", formattedWords)
})

return formattedInput

}

please some one help me..!

0

There are 0 best solutions below