小编use*_*022的帖子

字符串替换时单词中的撇号无法识别

我在用正则表达式替换“you\re”一词时遇到问题。

\n\n

所有其他单词都正确更改,只是单词"you\'re"。\n我认为它在撇号之后没有解析。

\n\n

我必须将单词“you”替换为“I”,将“you\'re”替换为“I\'m”。\n这会将“you”更改为“I”,但“you\re”会变为“I\” 're'因为它没有超过撇号,并且由于某种原因它认为这是单词的结尾。我必须以某种方式逃避撇号。

\n\n

请参阅下面的相关代码。

\n\n
package main\n\nimport (\n    "fmt"\n    "math/rand"\n    "regexp"\n    "strings"\n    "time"\n)\n\n//Function ElizaResponse to take in and return a string\nfunc ElizaResponse(str string) string {\n\n    //  replace := "How do you know you are"\n\n    /*Regex MatchString function with isolation of the word "father"\n    *with a boundry ignore case regex command.\n     */\n    if matched, _ := regexp.MatchString(`(?i)\\bfather\\b`, str);\n …
Run Code Online (Sandbox Code Playgroud)

regex go regex-group

1
推荐指数
1
解决办法
921
查看次数

标签 统计

go ×1

regex ×1

regex-group ×1