Hi All,

jmerelo on the chat line asked me to place this on Stack Overflow.
I will once I figure it out.  In the mean time, here are my
notes on string subsitutions:

-T

Perl6: string subsitutions:

Reference: https://docstore.mik.ua/orelly/perl/cookbook/ch06_16.htm


Note s:g:i
  s for subsitute
  g for global
  i for case insensititive
  $ match at the end of the string


$ perl6 -e 'my $x="abc/afg/ahi"; $x ~~ s:g/a/r/; say $x'
rbc/rfg/rhi

$ perl6 -e 'my $x="abc/Afg/ahi"; $x ~~ s:g:i/a/r/; say $x'
rbc/rfg/rhi



# greedy pattern
s/<.*>//gs;                     # try to remove tags, very badly

# non-greedy pattern
s/<.*?>//gs;                    # try to remove tags, still rather badly


Perl 6 examples of greedy:

perl6 -e 'my $x="abc/def/ghi"; $x ~~ s:|.*"/"||; say $x'
ghi

$ perl6 -e 'my $x="abc/def/ghi"; $x ~~ s:g/.*"/"//; say $x'
ghi

$ perl6 -e 'my $x="abc/def/ghi"; $x ~~ s/.*"/"//; say $x'
ghi

$ perl6 -e 'my $x="abc/def/ghi"; $x ~~ s/.*\///; say $x'
ghi


spaces:
$ perl6 -e 'my $x="abc  ghi"; $x ~~ s/.*abc"  "//; say $x'
ghi


Replace a tab and a line feed with a space:
$Clipboard ~~ s:global/ "\t" / /;
$Clipboard ~~ s:global/ "\n" / /;


Remove leading and trailing spaces:

perl6 -e 'my $x=" State : abc "; $x ~~ s/.*?" : "//; $x ~~ s/" ".*//; say "<$x>";'
<abc>

my $x=" State : abc "; $x = $x.subst(/.*?" : "/, "").subst(/ " "+ /,""); say "<$x>";
<abc>


Remove the last letter:
$ perl6 -e 'my $x="a b c d "; $x = chop( $x ); say "<$x>";'
<a b c d>

$ perl6 -e 'my $x="a b c d "; $x ~~ s/" "$//; say "<$x>";'
<a b c d>
Note "$" means to match at the end of the string


Multiple of a single test (if $Terminal = xterm OR $Terminal = linux):
if $Terminal ~~ /xterm || linux/

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to