[/D/S] vs. [^/d/s]

[/D/S] vs. [^/d/s]

am 29.11.2007 20:48:01 von landemaine

Hello,

I'm reading this excellent tutorial: http://www.regular-expressions.info/charclass.html
However in the "Negated Shorthand Character Classes" section, I didn't
understand this:

"Be careful when using the negated shorthands inside square brackets.
[\D\S] is not the same as [^\d\s]. The latter will match any character
that is not a digit or whitespace. So it will match x, but not 8. The
former, however, will match any character that is either not a digit,
or is not whitespace. Because a digit is not whitespace, and
whitespace is not a digit, [\D\S] will match any character, digit,
whitespace or otherwise".

Doesn't it look like the same thing? Please let me know!
Thanks,

--
Charles.

Re: [/D/S] vs. [^/d/s]

am 29.11.2007 22:19:24 von Ed Morton

On 11/29/2007 1:48 PM, Charles A. Landemaine wrote:
> Hello,
>
> I'm reading this excellent tutorial: http://www.regular-expressions.info/charclass.html
> However in the "Negated Shorthand Character Classes" section, I didn't
> understand this:
>
> "Be careful when using the negated shorthands inside square brackets.
> [\D\S] is not the same as [^\d\s]. The latter will match any character
> that is not a digit or whitespace. So it will match x, but not 8. The
> former, however, will match any character that is either not a digit,
> or is not whitespace. Because a digit is not whitespace, and
> whitespace is not a digit, [\D\S] will match any character, digit,
> whitespace or otherwise".
>
> Doesn't it look like the same thing? Please let me know!

No, it doesn't. It's the same as the difference between:

"(not 7) OR (not 3)" versus "not (7 OR 3)"

By applying boolean algebra the former is equivalent to "not (7 AND 3)" which of
course is any number since no number is both 7 and 3.

In your case, "[\D\S]" means "(not digit) OR (not space)" is equivalent to "not
(digit AND space)" which is any character since no character is both a digit and
a space, whereas "[^\d\s]" means "not (digit OR space)" which is only the
characters that are neither a digit nor a space (e.g. an alphabetic character or
a punctioanion mark, or...).

Ed.

Re: vs. [^/d/s]

am 03.12.2007 12:12:40 von landemaine

On Nov 29, 6:19 pm, Ed Morton wrote:
> No, it doesn't. It's the same as the difference between:
>
> "(not 7) OR (not 3)" versus "not (7 OR 3)"
>
> By applying boolean algebra the former is equivalent to "not (7 AND 3)" which of
> course is any number since no number is both 7 and 3.
>
> In your case, "[\D\S]" means "(not digit) OR (not space)" is equivalent to "not
> (digit AND space)" which is any character since no character is both a digit and
> a space, whereas "[^\d\s]" means "not (digit OR space)" which is only the
> characters that are neither a digit nor a space (e.g. an alphabetic character or
> a punctioanion mark, or...).
>


Thanks Ed for the explanatin ;)

--
Charles.