I am trying to place an escape sign before every non-alphanumeric character:
> my $b = "!@#%^||" ~ "/welcome xyz:!@#\$%^&*()|:;.,?/-."
!@#%^||/welcome xyz:!@#$%^&*()|:;.,?/-.
> my $c = $b.subst(/<:!L + :!N - [./-]>/, "\\" ~ $/, :g)
\ \ \ \ \ \ \ /welcome\ xyz\ \ \ \ \ \ \ \ \ \ \ \ \ \ .\ \ /-.
This is the result after running the code the first time. After I run the code the second time, the results is a long string of repeating matches. Similar result if I use the "?" quantifier.
> my $c = $b.subst(/<:!L + :!N - [./-]>/, "\\" ~ $/, :g)
\! @ # % ^ | | : ! @ # $ % ^ & * ( ) | : ; , ?\! @ # % ^ | | : ! @ # $ % ^ & * ( ) | : ; , ?\! @ # % ^ | | # This is truncated long string of undesired result.
Then I tried comb to substitute a single char, but I get multiple errors
> $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 \ \: \! \@ \# \$ \% \^ \& \* \( \) \| \: . \ \, / - .)
And the result is slightly different if I run the code a second time:
(\ \! \@ \# \% \^ \| / w e l c o m e \ x y z \ \: \! \@ \# \$ \% \^ \& \* \( \) \| \: . \ \, / - .)
Also I cannot join this list:
> $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
The routine tr/// does not do what I try to accomplish.
What is a quick way to place "\" before every non-alnum char in a string? Seems so simple yet so hard. Thanks.