将JS替换为Golang JS Regexp

发布于 2025-02-07 13:07:10 字数 1552 浏览 1 评论 0原文

在JavaScript中,我有一个Word替换函数,我需要转换为Golang,但似乎并没有替换。我相信我在Golang的正则是有问题的。

工作js示例 - 用完整单词代替街道名称缩写。 我不能简单地替换,因为某些单词在字符串的末尾,有些单词在之前和之后都有空白空间,而有些单词在字符串的开头,因此我的不同regexp。

 word.replace(/ RD$/, ' ROAD')
    .replace(' RD ', ' ROAD ')
    .replace(' MKT ', ' MARKET ')
    .replace(' NTH ', ' NORTH ')
    .replace(/^NTH /, 'NORTH ')
    .replace(/ NTH$/, ' NORTH')
    .replace(' PK ', ' PARK ')
    .replace(/^STH /, 'SOUTH ')
    .replace(' STH ', ' SOUTH ')
    .replace(' ST ', ' STREET ')

我与Golang的尝试。

type Address struct {
   .....
   StreetName string
}

func (address *Address) replace(oldWord string, newWord string) {
    sampleRegexp := regexp.MustCompile(oldWord)

    result := sampleRegexp.ReplaceAllString(string(address.StreetName), newWord)

    fmt.Println(oldWord, newWord, result)
    address.StreetName = result

}

然后在我的主机上。例如,我称其为这样的,

    address.replace(`/ RD$/`, " ROAD")
    address.replace(" RD ", " ROAD ")
    address.replace(" MKT ", " MARKET ")
    address.replace(" NTH ", " NORTH ")
    address.replace(`/^NTH /`, "NORTH ")
    address.replace(`/ NTH$/`, " NORTH")
    address.replace(" PK ", " PARK ")
    address.replace(`/^STH /`, "SOUTH ")

例如,如果StreetName为Market Rd,我希望它成为Market道路,但没有替换。

可能是因为REGEXP不为字符串

我做了一个简单的 Playground

In Javascript I have a word replace function that I need to convert to GoLang, but it doesn't seems to be replacing. I believe there's something wrong with my regex in Golang.

Working JS Example - Replacing street names acronyms with the full word.
I cannot do plain replace because some of the words are at the end of the string, some of the words have white space before and after, and some are at the start of the string, hence my different regexp.

 word.replace(/ RD$/, ' ROAD')
    .replace(' RD ', ' ROAD ')
    .replace(' MKT ', ' MARKET ')
    .replace(' NTH ', ' NORTH ')
    .replace(/^NTH /, 'NORTH ')
    .replace(/ NTH$/, ' NORTH')
    .replace(' PK ', ' PARK ')
    .replace(/^STH /, 'SOUTH ')
    .replace(' STH ', ' SOUTH ')
    .replace(' ST ', ' STREET ')

My attempt with GoLang.

type Address struct {
   .....
   StreetName string
}

func (address *Address) replace(oldWord string, newWord string) {
    sampleRegexp := regexp.MustCompile(oldWord)

    result := sampleRegexp.ReplaceAllString(string(address.StreetName), newWord)

    fmt.Println(oldWord, newWord, result)
    address.StreetName = result

}

Then in my main.go I'm calling it like that

    address.replace(`/ RD$/`, " ROAD")
    address.replace(" RD ", " ROAD ")
    address.replace(" MKT ", " MARKET ")
    address.replace(" NTH ", " NORTH ")
    address.replace(`/^NTH /`, "NORTH ")
    address.replace(`/ NTH$/`, " NORTH")
    address.replace(" PK ", " PARK ")
    address.replace(`/^STH /`, "SOUTH ")

For example if the StreetName is Market Rd, i expect it to become Market Road, but it's not replacing.

Could it be because regexp is not of type string ?

I did up a simple playground

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪少女 2025-02-14 13:07:10

您为什么不使用零length Word Boundare \ b?这将把比赛锚定在任何单词边界上。

那只是一个问题:

https://go.dev/play/pplay/p/tks1niarzwq

func expandAbbreviations(s string) string {
  return rx.ReplaceAllStringFunc(s, func(m string) string {
    r := expansions[strings.ToLower(m)]
    if r == "" {
            r = m
        }
        return r
    })
}

var rx = regexp.MustCompile(`\b(?i:rd|mkt|nth|pk|sth|st)\b`)
var expansions = map[string]string{
    "rd":  "ROAD",
    "mkt": "MARKET",
    "nth": "NORTH",
    "pk":  "PARK",
    "sth": "SOUTH",
    "st":  "STREET",
}

转换

Nth 123 Pk St Sth

NORTH 123 PARK STREET SOUTH

Why don't you use the zero-length word boundary \b? That will anchor the match at any word boundary.

Then it's just a matter of:

https://go.dev/play/p/TKs1nIaRZWQ

func expandAbbreviations(s string) string {
  return rx.ReplaceAllStringFunc(s, func(m string) string {
    r := expansions[strings.ToLower(m)]
    if r == "" {
            r = m
        }
        return r
    })
}

var rx = regexp.MustCompile(`\b(?i:rd|mkt|nth|pk|sth|st)\b`)
var expansions = map[string]string{
    "rd":  "ROAD",
    "mkt": "MARKET",
    "nth": "NORTH",
    "pk":  "PARK",
    "sth": "SOUTH",
    "st":  "STREET",
}

which converts

Nth 123 Pk St Sth

to

NORTH 123 PARK STREET SOUTH
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文