liveBook · Manning

Character class

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

A character class is a regular-expression construct that matches a character selected from, or excluded by, a specified set or range.

A character class is a regular-expression construct that matches characters from a specified category or set. Square brackets can combine categories such as word and nonword characters, making a pattern tolerate text such as hyphens. section 21.4

Common character-class patterns include:

  • \w matches a word character, while \W matches a nonword character. section 21.4
  • [\W\w] combines nonword and word characters, so the match can include characters that \w alone would miss. section 21.4
  • A ^ inside square brackets indicates a negative match; outside brackets, ^ anchors the match to the beginning of the string. section 21.2

Finding computer-account logons

To find security events for computer accounts beginning with WIN and ending with TM20$ through TM40$, use WIN[\W\w]+TM[234][0-9]\$. The [\W\w] portion allows a hyphen in the account name, while [234] and [0-9] restrict the ending digits. section 21.4

PS C:\> get-eventlog -LogName security | where { $_.eventid -eq 4624 } | 
select -ExpandProperty message | select-string -pattern 
"WIN[\W\w]+TM[234][0-9]\$"

Run the PowerShell search against security-log messages and inspect the matching account names. section 21.4

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Demonstrates \w, \W, combined classes, numeric ranges, and a practical PowerShell pattern without developing regex internals.
Examples
Uses computer-account names in Windows security events and log lines containing mixed word, nonword, and numeric characters.
What it assumes
Assumes basic regular-expression syntax and PowerShell piping, while demonstrating character classes through concrete searches.
What you can do after
Apply it via PowerShell Select-String to locate log messages whose characters meet practical account-name or log-text constraints.
How it connects
Connects character classes with regular expressions, anchors, quantifiers, and PowerShell Select-String.

Also explained in

Compare how 9 books explain character class →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.