Skip to content

Instantly share code, notes, and snippets.

@nd3i
Created December 14, 2024 17:18
Show Gist options
  • Select an option

  • Save nd3i/90789f85b4d53f945cc0ca2ab2859095 to your computer and use it in GitHub Desktop.

Select an option

Save nd3i/90789f85b4d53f945cc0ca2ab2859095 to your computer and use it in GitHub Desktop.
Question re a string containing a regex
## Question: why do #s 1 & 2 work, but #3 does not?
##
my @w = <cat bat rat>;
my $s = 'the cattle was rattle by the battery';
say '1: ', $s.subst(/ <|w>( @w )\w* /, {$0}, :g);
## Output> the cat was rat by the bat
say '2: ', $s.subst(/ <|w>( cat || rat || bat )\w* /, {$0}, :g);
## Output> the cat was rat by the bat
my $re = '<|w>( cat || rat || bat )\w*';
say '3: ', $s.subst(/ <$re> /, {$0}, :g);
## Output> Use of Nil in string context ... (3x)
## Output> the was by the
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment