我正在尝试在每个非字母数字字符之前放置一个转义符号:
> my $b = "!@#%^||" ~ "/welcome xyz:!@#\$%^&*()|:;.,?/-."
!@#%^||/welcome xyz:!@#$%^&*()|:;.,?/-.
> my $c = $b.subst(/<:!L + :!N - [./-]>/, "\\" ~ $/, :g)
\ \ \ \ \ \ \ /welcome\ xyz\ \ \ \ \ \ \ \ \ \ \ \ \ \ .\ \ /-.
第一次运行代码后,这是结果。第二次运行代码后,结果是一长串重复匹配的字符串。如果使用“?”量词,结果类似。
> my $c = $b.subst(/<:!L + :!N - [./-]>/, "\\" ~ $/, :g)
\! @ # % ^ | | : ! @ # $ % ^ & * ( ) | : ; , ?\! @ # % ^ | | : ! @ # $ % ^ & * ( ) | : ; , ?\! @ # % ^ | | # This is truncated long string of undesired result.
然后我尝试使用comb替换单个字符,但是我得到了多个错误。
> $b.comb.map( {.subst(/<:!L + :!N - [./-]>/, "\\" ~ $/)} )
Use of uninitialized value element of type Any in string context.
Methods .^name, .raku, .gist, or .say can be used to stringify it to something meaningful.
in block at <unknown file> line 1
(\! \! \@ \# \% \^ \| / w e l c o m e \ x y z \ \: \! \@ \# \$ \% \^ \& \* \( \) \| \: . \ \, / - .)
如果我第二次运行代码,结果会稍有不同:
(\ \! \@ \# \% \^ \| / w e l c o m e \ x y z \ \: \! \@ \# \$ \% \^ \& \* \( \) \| \: . \ \, / - .)
同时,我无法加入这个列表:
> $b.comb.map( { if $_.so { .subst(/<:!L + :!N - [./-]>/, "\\" ~ $/)} } ).join
Use of uninitialized value element of type Any in string context.
Methods .^name, .raku, .gist, or .say can be used to stringify it to something meaningful.
in block at <unknown file> line 1
in block <unit> at <unknown file> line 1
例行的 tr/// 函数没有完成我想要实现的功能。
如何快速将字符串中每个非字母数字字符前面添加 "\" ? 看起来很简单,但实际上很困难。谢谢。
<alnum>
在Raku中包括下划线_
。因此,任何下划线_
都不会被您的解决方案反斜杠转义(这可能确实是@lisprogtor想要的)。 - jubilatious1\\
- 啊...一个单独的\
转义了闭合的'
。引号很棘手。我建议使用Q'\'
来避免这个查询。 - librasteve