12

In trying to answer the question Writing text into new line when a particular character is found, I have employed Regexp::Grammars. It has long interested me and finally I had reason to learn. I noticed that the description section the author has a LaTeX parser (I am an avid LaTeX user, so this interested me) but it has one odd construct seen here:

    <rule: Option>     [^][\$&%#_{}~^\s,]+

    <rule: Literal>    [^][\$&%#_{}~^\s]+

What do the [^] character classes accomplish?

Community
  • 1
  • 1
Joel Berger
  • 20,180
  • 5
  • 49
  • 104

1 Answers1

23

[^][…] is not two character classes but just one character class containing any other character except ], [, and (see Special Characters Inside a Bracketed Character Class):

However, if the ] is the first (or the second if the first character is a caret) character of a bracketed character class, it does not denote the end of the class (as you cannot have an empty class) and is considered part of the set of characters that can be matched without escaping.

Examples:

"+"   =~ /[+?*]/     #  Match, "+" in a character class is not special.
"\cH" =~ /[\b]/      #  Match, \b inside in a character class
                     #  is equivalent to a backspace.
"]"   =~ /[][]/      #  Match, as the character class contains.
                     #  both [ and ].
"[]"  =~ /[[]]/      #  Match, the pattern contains a character class
                     #  containing just ], and the character class is
                     #  followed by a ].
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • @Gumbo If you can state with certainty that this is how Perl parses the Regex, I will delete my (incorrect) answer. Are you certain? – Phrogz Jun 13 '11 at 15:16
  • @Phrogz, this is how Perl handles it. – Qtax Jun 13 '11 at 15:17
  • @Gumbo, that seems to match more with what the LaTeX parser would need, is that in the docs somewhere?!?! – Joel Berger Jun 13 '11 at 15:17
  • @Gumbo - In character class `[` and `]` have to be escaped always. I don't think what you say is the case here. – manojlds Jun 13 '11 at 15:17
  • 1
    @manojlds no they don't, not in Perl anyway when used like this. _The 117k guy_ knows what he's talking about. ;) – Qtax Jun 13 '11 at 15:19
  • @Gumbo @Qtax - thanks. But is definitely a weird thing to have. I would never use such a regex! – manojlds Jun 13 '11 at 15:24
  • @Gumbo, `[^]` is obviously not pointless in JavaScript since there is no `/s`. ;) – Qtax Jun 13 '11 at 15:27