0

I have one use case where I can have a text string which can contain anything. What I want to achieve is to replace a certain pattern within that given string.

Let's say I have given string as

:es1
es2
aaes1aa
:es3,
es1:
ees1,
ees1 

{
"es1 :

What I am trying to do is here is suppose I have to replace all es1 in this string but with one condition. It has to be either end or start with [, | ; | : | " | ' | \\ | \s]. :es1, es1,, es1: and so on are accepted but eees1sss is not.

I tried ([, | ; | : | " | ' | \\ | \s])(es1)([, | ; | : | " | ' | , | \s]) something like this but I don't think it's what I need.

Go program:

match := regexp.MustCompile(`([, | ; | : | " | ' | \\ | \s])(es1)([, | ; | : | " | ' | , | \s])`)
    test := `:es1
    es2
    aaes1aa
    :es3,
    es1:
    ees1,
    ees1 
    
    {
    "es1 :`
    
    fmt.Println(match.ReplaceAllString(test, "$1es4$3"))

output:

       es2
        aaes1aa
        :es3,
:
        ees1,
        ees1 

        {
         :

I was expecting my output to be more like

:es4
    es2
    aaes1aa
    :es3,
    es4:
    ees1,
    ees1 

    {
    "es4 :
3
  • What about e:es1? Commented Sep 2, 2021 at 8:15
  • should match. since there is a delimiter in the back of es1. but only if e:es1 ends with one of delimiter as well or is last character in the string Commented Sep 2, 2021 at 8:19
  • first word and last are also allowed. es1: or :es1 in text es1: .....some text. ..:es1 Commented Sep 2, 2021 at 8:23

2 Answers 2

2

the solution provided below is not well tested against all possibilities, but it seems to be working.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    match := regexp.MustCompile(`([, | ; | : | " | ' | \\ | \s])es1|^es1([, | ; | : | " | ' | , | \s])`)
    test := `:es1
    es2
    aaes1aa
    :es3,
    es1:
    ees1,
    ees1 
    
    {
    "es1 :`

    fmt.Println(match.ReplaceAllString(test, "${1}es4${2}"))
}

https://play.golang.org/p/E8lb9vmM_Sa

Sign up to request clarification or add additional context in comments.

Comments

1

You can use

package main

import (
    "fmt"
    "regexp"
)

func main() {
    match := regexp.MustCompile(`([,;:"'\\\s])es1\b|\bes1([,;:"'\\\s])`)
    test := ":es1\n    es2\n    aaes1aa\n    :es3,\n    es1:\n    ees1,\n    ees1 \n    \n    {\n    \"es1 :"
    fmt.Println(match.ReplaceAllString(test, "${1}es4$2"))
}

See the Go demo and the regex demo. Note that the spaces and | chars inside square brackets are meaningful and match these chars literally, thus, you need to remove them all from your pattern.

The regex matches:

  • ([,;:"'\\\s])es1\b - Group 1: a comma, or a semi-colon, colon, double or single quotation mark, backslash or whitespace; then es1 as a whole word (\b is a word boundary)
  • | - or
  • \bes1 - a whole word es1
  • ([,;:"'\\\s]) - Group 2: a comma, or a semi-colon, colon, double or single quotation mark, backslash or whitespace

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.