doujizhong8352 2018-02-16 06:21
浏览 31
已采纳

搜索不区分大小写并替换整个单词

I need to search for specific pattern and only if its whole word or combination of few words I should replace it. I am struggling with metacharacters Say my search pattern is: "corp." Should be replaced with "Corporation" so when input: "SS Corp. Ltd" expected output is "SS Corporation Ltd"

I tried using:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    search :="corp."
    rep := "Corporation"
    sample :="SS Corp. LTd"
    var re = regexp.MustCompile(`(^|[^_])\b`+search+`\b([^_]|$)`)
    s2 := re.ReplaceAllString(sample, "${1}"+rep+"${2}")
}
  • 写回答

1条回答 默认 最新

  • duanba7653 2018-02-16 08:11
    关注

    There are several problems here:

    1. An unescaped . matches any char other than line break, it must be escaped. Since you are building the pattern dynamically, use regexp.QuoteMeta
    2. As a \b word boundary after . requires a word char, you can't expect a\.\b to match a. b. Replace the boundaries with (^|[^\p{L}0-9_]) for the leading boundary and ([^\p{L}0-9_]|$) for the trailing boundary.
    3. At this stage, the pattern will be built like this: `(?i)(^|[^\p{L}0-9_])`+regexp.QuoteMeta(search)+`([^\p{L}0-9_]|$)`, but since both the boundaries are consuming patterns, you will never match consecutive matches (corp. corp. will result in Corporation corp., the second one won't be replaced). You should repeat replacing until no regex match can be found.
    4. And to make the pattern case insensitive, use (?i) inline modifier at the pattern start.

    The regex will look like

    (?i)(^|[^\p{L}0-9_])corp\.([^\p{L}0-9_]|$)
    

    See the regex demo.

    Details

    • (?i) - case insensitive modifier
    • (^|[^\p{L}0-9_]) - either start of string or a char other than a Unicode letter, ASCII digit and _
    • corp\. - a corp. substring
    • ([^\p{L}0-9_]|$) - either a char other than a Unicode letter, ASCII digit and _ or end of string

    See this example demo:

    package main
    
    import (
        "fmt"
        "regexp"
    )
    
    func main() {
        search :="corp."
        rep := "Corporation"
        sample :="SS Corp. Corp. LTd"
        var re = regexp.MustCompile(`(?i)(^|[^\p{L}0-9_])`+regexp.QuoteMeta(search)+`([^\p{L}0-9_]|$)`)
        fmt.Println(re)
        var res = sample
        for re.MatchString(res) {
            res = ReplaceWith(res, re, "${1}"+rep+"${2}")
        }
        fmt.Println(res)
    }
    
    func ReplaceWith(s string, re *regexp.Regexp, repl string) string {
        return re.ReplaceAllString(s, repl)
    }
    

    Result: SS Corporation Corporation LTd.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?