Products
GG网络技术分享 2025-03-18 16:15 7
I have a function in which I need to replace \"byte\" with \"$ball\". This doesn\'t seem to work correctly.
Here is the program snippet.fun main() {str := []byte(\"$apple in a byte
\")
strReplace := \"$ball\"
re := regexp.MustCompile(\"byte\")
final := re.ReplaceAll(str, []byte(strReplace))
ioutil.WriteFile(\"testfile.txt\", final, 0744)
}
Expected Output in testfile.txt:
$apple in a $ballActual Output in testfile.txt:
$apple in aAny solutions for successfully getting the desired output?
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议我有一个函数需要将“ byte”替换为“ $ ball”。 这似乎无法正常工作。
这是程序段。</ p>
fun main(){str:= [] byte(“ $ apple in 一个字节
“)
strReplace:=” $ ball“
re:= regexp.MustCompile(” byte“)
final:= re.ReplaceAll(str,[] byte(strReplace))
ioutil.WriteFile(“ testfile.txt”,最终版本0744)
}
</ code> </ pre>
testfile.txt中的预期输出:
$ apple in testfile.txt中的$ ball </ p>
实际输出:
</ p>
中的$ apple有成功解决所需输出的任何解决方案吗?</ p >
</ div>
网友观点:
You use $$
which is exactly what the documentation tells you.
The godoc section for ReplaceAll
(https://godoc.org/regexp#Regexp.ReplaceAll) tells you:
$ signs are interpreted as in Expand
So reading the section for Expand
has the answer. https://godoc.org/regexp#Regexp.Expand
Please throughly read all relevant documentation for the package and functions you are using before posting a question.
新手上路:图文解读助你理解和使用正则表达式
这篇博客是关于正则表达式(regex)的插图指南,旨在为那些从来没有使用过正则表达式,想尝试但又望而生畏的新手提供一个简单介绍。
选自http://janmeppe.com,作者:Jan Meppe,机器之心编译,参与:韩放、杜伟。
所以,欢迎使用正则表达式…
对于大多数没有接受过正式 CS 教育的人来说,正则表达式似乎只有最核心的 Unix 程序员才敢碰。
一个好的正则表达式看起来像魔法,但请记住:任何足够先进的技术都无法与魔法区分开来。
所以,就让我们揭开正则表达式的神秘面纱!
如果你理解正则表达式,它会突然变成一个超快速和强大的工具……但你首先需要理解它,老实说,我觉得新手可能会对它望而生畏!
让我们从基础开始。正则表达式(regex)是什么?它们的用途是什么?
Regex 新手上路
本质上来看,正则表达式是定义一种搜索模式的字符序列。
正则表达式通常用于 grep 等工具中,以在较长文本字符串中查找模式。
考虑以下一个 cat.txt 文件:
Demand feedback