用文字\\ n替换newline?
我如何用raku中的文字'\ n'
替换新线?我尝试了s/\ n/\\ n/
,我期望工作但无效。
谢谢。
How would I replace a newline with a literal '\n'
in raku? I tried s/\n/\\n/
which I expected to work but did not.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
continue
If you read Shakespeare's Sonnet 18 into Raku
slurp
-wise, (a.k.a. all at once) this is what you'll get:Above you see embedded
\n
newlines whenslurp
ing (visualized with a call to.raku
, giving you an idea how Raku represents objects internally). But if you read the file inline
-wise, by default Raku one-liners implement newline processing identical to Perl's-l
command-line flag. Which is to say,\n
newlines are stripped from input, and added back during output (if so desired). So this is what you'll get when readingline
-wise:Note above,
\n
newlines are removed (chomp
ed) off of each line. A more readable output is obtained iterating via afor
loop:Which is same as dropping
for lines
and changing command-line flag(s) from-e
to-ne
:So the first question I have is whether
\n
newlines actually exist in your Raku text object(s). If so, you can double-escape them with the code as follows (adding\
backslashes as required by your shell):OTOH, if you don't have true
\n
newlines in your Raku text object(s), you can simply append them (or any other text--such as\\n
). Use~
tilde for string concatenation, and add\
backslashes as required by your shell:Finally, I have to put this here just in case someone has the far-more-common, opposite issue: having to remove/correct
\\n
embedded characters from a text file. Using Raku: