Negated character class

Within a character class, the caret (^) is used to negate the class. The character class, [aeiou], matches vowels, negating it with the caret, [^aeiou], which matches any character except a vowel (including spaces, punctuation, tabs, and everything else).

As with the hyphen, the caret is only effective if it is in the right position. In this case, it only negates the class if it is the first character. Elsewhere in the class, it is a literal character.

A negated character class is sometimes the fastest way to tackle a problem. If the list of expected characters is small, negating that list is a quick way to perform a match.

In the following example, the negated character class is used with the -replace operator to fix a problem:

'Ba%by8 a12315tthe1231 k#.,154eyboard' -replace '[^a-z ]' 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.118.99