Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl6 negating user-defined caracter class

I am trying to ignore all lines that has a quotation mark in it, somehow it is kind of onfusing:

> my $y='\"\""';
\"\""
> so $y ~~ m/<-[\"]>/
True                      # $y has a " mark, so I want it to be False
> $y ~~ m/<-[\"]>/
「\」
>  $y ~~ m:g/<-[\"]>/
(「\」 「\」)
> $y ~~ m:g/<-["]>/
(「\」 「\」)
$y ~~ m/<-[\\]>/
「"」
> $y ~~ m/<-[\\\"]>/
False

Is <-[\"]> the same as <-["]> ?

> say '"in quotes"' ~~ / '"' <-[ " ]> * '"'/;
「"in quotes"」
> say 'no "foo" quotes' ~~ /  <-[ " ]> + /;
「no 」
> say 'no "foo" quotes' ~~ /  <-[ \" ]> + /;
「no 」

In perl6 documentation example, https://docs.perl6.org/language/regexes#Wildcards_and_character_classes , the author did not have to escape the quotation mark; however, I do have to escape for it to work, <-[\\"]> , i.e., escape \ and escape ". What did I misunderstand?

Thanks !!

like image 597
lisprogtor Avatar asked Nov 29 '25 01:11

lisprogtor


1 Answers

You don't need to escape characters inside a character class specification, except the backslash itself: So specifying <-[\"]> is the same as <-["]>. And specifying <-[\\"]>, indicates all characters except \ and ".

However, there may be a simpler way for you: if you're looking for just a single (set of) character(s) in a string, there's contains:

my $y = "foo bar baz";
say $y.contains("oo");    # True

This bypasses all of the expensive regex / grammar machinery by using a single simple low-level string match.

like image 179
Elizabeth Mattijsen Avatar answered Dec 01 '25 21:12

Elizabeth Mattijsen