in reply to RegEx: Why is [.] not a valid character class?
Why is '.' not allowed within a character class?
It is a valid character class. It matches only a dot.
You seem to be asking "Why is . not a metacharacter in a character class?" Just because. Meta stuff in character classes seems to be limited to backslash+letter.
All good questions. I have no answer to them... Except from what I hear, Larry would change a few things in perl6 — so you're not alone in your gripe. One idea was letting . match anything (including newline), and \N match anything but newline (the current .). I don't know what the current projections for perl6 are, as I don't actively follow its evolution.It's clear to me now that my desired character class [.\n] can be achieved with the s modifier - but why is there such an "inconsistent way" using a modifier to emulate a character class?
Why is there no "super" character class - matching ALL characters including '\n'?
What's the reason excluding '\n' from '.'? (Why is '\n' handled in a special way?)
An excuse for the things being the way they are, is that Larry thought no normal string contains newlines, as perl was originally mainly intended to do line-by-line processing of files, and the only place you could have a newline, is at the end of the string. That's why for example, $ is allowed to match just in front of a newline at the end of a string. Another example on how a newline is different.
BTW you can localize the effect of /s by using the (?s:PATTERN) syntax. That is: add options between the question mark and the colon, in the syntax for non-capturing parens. Put "-" in front of options you want disabled. You can experiment using
whenever you forgot the syntax, again — it inserts those options.print qr/./s;
|
|---|