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?

1 Answer 1

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 ]. 
Sign up to request clarification or add additional context in comments.

7 Comments

@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, this is how Perl handles it.
@Gumbo, that seems to match more with what the LaTeX parser would need, is that in the docs somewhere?!?!
@Gumbo - In character class [ and ] have to be escaped always. I don't think what you say is the case here.
@manojlds no they don't, not in Perl anyway when used like this. The 117k guy knows what he's talking about. ;)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.