Case insensitive string replace in Go

huangapple go评论96阅读模式
英文:

Case insensitive string replace in Go

问题

NewReplacer.Replace方法可以进行大小写不敏感的字符串替换吗?

r := strings.NewReplacer("html", "xml")
fmt.Println(r.Replace("This is HTML!"))

如果不能,那么在Go语言中进行大小写不敏感的字符串替换的最佳方法是什么?

英文:

Can NewReplacer.Replace do case insensitive string replacement?

r := strings.NewReplacer("html", "xml")
fmt.Println(r.Replace("This is <b>HTML</b>!"))

If not, what's the best way to do case insensitive string replace in Go?

答案1

得分: 14

你可以使用正则表达式来实现:

re := regexp.MustCompile(`(?i)html`)
fmt.Println(re.ReplaceAllString("html HTML Html", "XML"))

Playground:http://play.golang.org/p/H0Gk6pbp2c。

值得注意的是,大小写在不同的语言和区域可能会有所不同。例如,德语字母 "ß" 的大写形式是 "SS"。虽然这通常不会影响英文文本,但在处理多语言文本和需要处理它们的程序时,这是需要考虑的事情。

英文:

You can use regular expressions for that:

re := regexp.MustCompile(`(?i)html`)
fmt.Println(re.ReplaceAllString("html HTML Html", "XML"))

Playground: http://play.golang.org/p/H0Gk6pbp2c.

It's worth noting that case is a thing that can be different depending on the language and locale. For example, the capital form of German letter "ß" is "SS". While this doesn't generally influence English texts, this is something to bear in mind when working with multi-lingual texts and programs that need to work them.

答案2

得分: 2

一个通用的解决方案如下:

import (
    "fmt"
    "regexp"
)

type CaseInsensitiveReplacer struct {
    toReplace   *regexp.Regexp
    replaceWith string
}

func NewCaseInsensitiveReplacer(toReplace, replaceWith string) *CaseInsensitiveReplacer {
    return &CaseInsensitiveReplacer{
        toReplace:   regexp.MustCompile("(?i)" + toReplace),
        replaceWith: replaceWith,
    }
}

func (cir *CaseInsensitiveReplacer) Replace(str string) string {
    return cir.toReplace.ReplaceAllString(str, cir.replaceWith)
}

然后通过以下方式使用:

r := NewCaseInsensitiveReplacer("html", "xml")
fmt.Println(r.Replace("This is <b>HTML</b>!"))

这里是一个在 playground 上的示例链接:链接

英文:

A generic solution would be as follows:

import (
    &quot;fmt&quot;
    &quot;regexp&quot;
)

type CaseInsensitiveReplacer struct {
    toReplace   *regexp.Regexp
    replaceWith string
}

func NewCaseInsensitiveReplacer(toReplace, replaceWith string) *CaseInsensitiveReplacer {
    return &amp;CaseInsensitiveReplacer{
	    toReplace:   regexp.MustCompile(&quot;(?i)&quot; + toReplace),
	    replaceWith: replaceWith,
    }
}

func (cir *CaseInsensitiveReplacer) Replace(str string) string {
    return cir.toReplace.ReplaceAllString(str, cir.replaceWith)
}

And then used via:

r := NewCaseInsensitiveReplacer(&quot;html&quot;, &quot;xml&quot;)
fmt.Println(r.Replace(&quot;This is &lt;b&gt;HTML&lt;/b&gt;!&quot;))

Here's a link to an example in the playground.

答案3

得分: 1

根据文档,它不支持

我不确定最佳方法是什么,但你可以使用正则表达式中的替换来实现,并通过i标志使其不区分大小写。

英文:

Based on the documentation it does not.

I am not sure about the best way, but you can do this with replace in regular expressions and make it case-insensitive with i flag

答案4

得分: 0

// 使用IReplace函数替换子字符串,不区分大小写。

import (
	"strings"
	"unicode/utf8"
)

func IReplace(s, old, new string) string {  // 替换所有匹配项,不区分大小写
	if old == new || old == "" {
		return s // 避免分配内存
	}
	t := strings.ToLower(s)
	o := strings.ToLower(old)

	// 计算替换的次数
	n := strings.Count(t, o)
	if n == 0 {
		return s // 避免分配内存
	}
	// 将替换应用到缓冲区
	var b strings.Builder
	b.Grow(len(s) + n*(len(new)-len(old)))
	start := 0
	for i := 0; i < n; i++ {
		j := start
		if len(old) == 0 {
			if i > 0 {
				_, wid := utf8.DecodeRuneInString(s[start:])
				j += wid
			}
		} else {
			j += strings.Index(t[start:], o)
		}
		b.WriteString(s[start:j])
		b.WriteString(new)
		start = j + len(old)
	}
	b.WriteString(s[start:])
	return b.String()
}
英文:

> func IReplace to replace the substring is not case sensitive.
>
> Using a regular expression (as mentioned in previous posts) is not
> correct for all cases. Eg: for parameters: ("Alpha=? BETA",
> "ALPHA=?", ALPHA=1) the result should be "ALPHA=1 BETA", but it won't
> be.

import (
	&quot;strings&quot;
	&quot;unicode/utf8&quot;
)

    func IReplace(s, old, new string) string {  // replace all, case insensitive
    	if old == new || old == &quot;&quot; {
    		return s // avoid allocation
    	}
    	t := strings.ToLower(s)
    	o := strings.ToLower(old)
    
    	// Compute number of replacements.
    	n := strings.Count(t, o)
    	if n == 0 {
    		return s // avoid allocation
    	}
    	// Apply replacements to buffer.
    	var b strings.Builder
    	b.Grow(len(s) + n*(len(new)-len(old)))
    	start := 0
    	for i := 0; i &lt; n; i++ {
    		j := start
    		if len(old) == 0 {
    			if i &gt; 0 {
    				_, wid := utf8.DecodeRuneInString(s[start:])
    				j += wid
    			}
    		} else {
    			j += strings.Index(t[start:], o)
    		}
    		b.WriteString(s[start:j])
    		b.WriteString(new)
    		start = j + len(old)
    	}
    	b.WriteString(s[start:])
    	return b.String()
    }

huangapple
  • 本文由 发表于 2015年7月11日 03:33:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/31348919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定