Go,regexp:匹配不区分大小写并保留原始文本。

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

Go, regexp : to match either case and keep the original text

问题

我想用新的字符串替换与正则表达式匹配的字符串,但仍然保留原始文本的一部分。

我想要得到:

I own_VERB it and also have_VERB it

I own it and also have it

我该如何用一行代码实现这个?我尝试过,但无法进一步。谢谢。

package main

import "fmt"
import "regexp"

func getverb(str string) string {
    var validID = regexp.MustCompile(`(own)|(have)`)
    return validID.ReplaceAllString(str, "_VERB")    
}

func main() {
    fmt.Println(getverb("I own it and also have it"))
    // 如何保留原始文本,例如
    // I own_VERB it and also have_VERB it
}
英文:

I want to replace regex-matched strings with new string but still keeping part of the original text.

I want to get

I own_VERB it and also have_VERB it

from

I own it and also have it

how do I do this with one line of code? I tried but can't get further than this. Thanks,

http://play.golang.org/p/SruLyf3VK_

      package main

      import "fmt"
      import "regexp"

      func getverb(str string) string {
      	var validID = regexp.MustCompile(`(own)|(have)`)
      	return validID. ReplaceAllString(str, "_VERB")	
      }

      func main() {
      	fmt.Println(getverb("I own it and also have it"))
      	// how do I keep the original text like
      	// I own_VERB it and also have_VERB it
      }

答案1

得分: 3

你甚至不需要为此使用捕获组。

package main

import "fmt"
import "regexp"

func getverb(str string) string {
    var validID = regexp.MustCompile(`own|have`)
    return validID.ReplaceAllString(str, "
package main
import "fmt"
import "regexp"
func getverb(str string) string {
var validID = regexp.MustCompile(`own|have`)
return validID.ReplaceAllString(str, "${0}_VERB")    
}
func main() {
fmt.Println(getverb("I own it and also have it"))
// 如何保留原始文本,例如
// I own_VERB it and also have_VERB it
}
_VERB") } func main() { fmt.Println(getverb("I own it and also have it")) // 如何保留原始文本,例如 // I own_VERB it and also have_VERB it }

${0} 包含与整个模式匹配的字符串;${1} 将包含与第一个子模式(或捕获组)匹配的字符串(如果有的话),你可以在 Darka 的回答中看到。

英文:

You don't even need a capture group for this.

package main

import "fmt"
import "regexp"

func getverb(str string) string {
	var validID = regexp.MustCompile(`own|have`)
	return validID. ReplaceAllString(str, "
package main
import "fmt"
import "regexp"
func getverb(str string) string {
var validID = regexp.MustCompile(`own|have`)
return validID. ReplaceAllString(str, "${0}_VERB")	
}
func main() {
fmt.Println(getverb("I own it and also have it"))
// how do I keep the original text like
// I own_VERB it and also have_VERB it
}
_VERB") } func main() { fmt.Println(getverb("I own it and also have it")) // how do I keep the original text like // I own_VERB it and also have_VERB it }

${0} contains the string which matched the whole pattern; ${1} will contain the string which matched the first sub-pattern (or capture group) if there is any, and which you can see in Darka's answer.

答案2

得分: 1

在替换过程中,$符号的解释与Expand中的解释相同,因此例如$1表示第一个子匹配的文本。

package main

import (
   "fmt"
   "regexp"
)

func main() {
    re := regexp.MustCompile("(own|have)")
    fmt.Println(re.ReplaceAllString("I own it and also have it", "_VERB"))		
}

输出结果:

I own_VERB it and also have_VERB it
英文:

>Inside replacement, $ signs are interpreted as in Expand, so for instance $1 represents the text of the first submatch.

package main

import (
   "fmt"
   "regexp"
)

func main() {
    re := regexp.MustCompile("(own|have)")
    fmt.Println(re.ReplaceAllString("I own it and also have it", "_VERB"))		
}

Output

I own_VERB it and also have_VERB it

答案3

得分: 1

似乎需要进行一些谷歌搜索:

var validID = regexp.MustCompile(`(own|have)`)
return validID.ReplaceAllString(str, "_VERB")

这段代码的作用是将字符串中的 "own" 或者 "have" 替换为 "${1}_VERB"。

英文:

Seems a bit googling helps:

var validID = regexp.MustCompile(`(own|have)`)
return validID. ReplaceAllString(str, "_VERB")

huangapple
  • 本文由 发表于 2013年10月2日 00:26:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/19121331.html
匿名

发表评论

匿名网友

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

确定