5

I was reading in regexes documemtation about "Tilde for nesting structures".

The sideline explanation about the use of <?> is:

Here <?> successfully matches the null string.

I assumed that I was able to use <?[]> instead of it, but it failed to do so!

As an example:

say so "" ~~ / <?> /;
say so "test" ~~ / <?> /;

say so "" ~~ / <?[]> /;
say so "test" ~~ / <?[]> /;

The response:

True
True

False
False

Could someone give me an explanation about this?

jakar
  • 1,701
  • 5
  • 14
  • 2
    It is in line with other regex engines, an empty character class matches *nothing*, i.e. it is a regex that will never match any string. – Wiktor Stribiżew Feb 27 '20 at 10:46

1 Answers1

10

The syntax <?[]> means a lookahead matching an empty character class. Observe that an empty character class also never matches:

say "x" ~~ /<[]>/   # Nil

A character class specifies a set of characters that could be matched. An empty character class implies an empty set of characters, and so cannot possibly match anything.

Jonathan Worthington
  • 29,104
  • 2
  • 97
  • 136