in reply to Re: regexp question
in thread regexp question

I would ask you the same question whats advantage in this case /[\d\s]/; meaning doesnt change outside of cc

Replies are listed 'Best First'.
Re^3: regexp question
by Anonymous Monk on Oct 28, 2011 at 09:37 UTC
    Of course it's different.
    [\d\s] # matches a single char - digit or whitespace \d\s # matches two chars - a digit then whitespace

        Those are the same.

        The whole point of something like \d is to be a convenient abbreviation. It would be irritating (and error prone) if you had to spell it out in full each time it was used inside []

        You're missing the whole point here. Square brackets are a character class. If I try to match on [a-z0-9], I'm specifying one character that falls in the class of characters from a-z and 0-9. That is, I'm trying to match one character that could be any of those in the class.

        But if I try to match on [.], I'm specifying one character that falls in the class of characters that are a period. In other words, [a-z] could match 'a', or 'b', or 'c', etc., but [.] can only ever match '.'. So [.] is exactly equal to '.' Thus it's a useless use of a character class.

        To use another example of yours, [\d\s] will match one character that is either a digit or a space character. It could match 9, or 8, or ' '. \d and \s retain their "magic" even in a character class. [\d] = \d = [0-9]

        The lesson here is don't use single-character classes.

        --marmot
Re^3: regexp question
by JavaFan (Canon) on Oct 28, 2011 at 12:24 UTC
    Because one can use \d and \s to build larger character classes. A dot already matches everything but a single character.

    What's the point of having such a character inside a character class? All you can do is build 4 different character classes: /[.]/, /[.\n]/, /[^.]/, /[^.\n]/. But they can all be written in a simple, different way: /./, /(?s:.)/, /\n/, and /(*FAIL)/

    So, once again, what would be the advantage of having dot inside a character class mean the same thing as outside of it?

      A dot already matches everything but a single character.

      Err... are you sure you meant to say that?

        Yes. Care to point out another character beside a newline that isn't matched by a dot?