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.

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?)

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.

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

print qr/./s;
whenever you forgot the syntax, again — it inserts those options.

In reply to Re: RegEx: Why is [.] not a valid character class? by bart
in thread RegEx: Why is [.] not a valid character class? by hoppfrosch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.