Golang regex extract from match

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

Golang regex extract from match

问题

我需要使用正则表达式来提取和替换一些文本。

需要替换以下内容的任何单词:

  • sub-19 -> u19
  • sub19 -> u19
  • (sub19) -> u19

我有以下正则表达式,但它缺少一个要求,无法完全解决:

(?i)sub(?:[^a-z|0-9])

英文:

I need to use regex to extract and replace some text.

Any word that has the following needs to be replaced:

  • sub-19 -> u19
  • sub19 -> u19
  • (sub19) -> u19

I have the following but it's missing one of the requirements and can't figure it out 100%

(?i)sub(?:[^a-z|0-9])

答案1

得分: 1

你可以在前后匹配非空白字符,并用第一个捕获组替换。

(?i)\S*sub-?(\d+)\S*

查看正则表达式演示

示例

package main
import (
    "regexp"
    "fmt"
)

func main(){
    var re = regexp.MustCompile(`(?i)\S*sub-?(\d+)\S*`)
    var str = `sub-19
sub19
(sub19)`
    
    fmt.Println(re.ReplaceAllString(str, "u$1"))
}

输出

u19
u19
u19

如果你不想删除前导和尾随的非空白字符,可以省略\S*并添加单词边界\b

(?i)\bsub-?(\d+)\b

查看另一个正则表达式演示

英文:

You can match non whitespace chars before and after and replace with group 1

(?i)\S*sub-?(\d+)\S*

See a regex demo

Example

package main
import (
    "regexp"
    "fmt"
)

func main(){
	var re = regexp.MustCompile(`(?i)\S*sub-?(\d+)\S*`)
    var str = `sub-19
sub19
(sub19)`
    
    fmt.Println(re.ReplaceAllString(str, "u$1"))
}

Output

u19
u19
u19

<hr>

If you don't want to remove leading and trailing non whitespace chars, you can omit the \S* and add a word boundary \b

(?i)\bsub-?(\d+)\b

See another regex demo

huangapple
  • 本文由 发表于 2021年6月9日 18:56:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/67902814.html
匿名

发表评论

匿名网友

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

确定