liveBook · Manning

Word character

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

A word character is a character included in the regular-expression word-character class, typically a letter, digit, or underscore.

A word character is any letter, number, or underscore matched by the regular-expression class \w. It excludes punctuation and whitespace; \W matches the opposite set. section 21.2

Use word-character classes to describe changing text in a pattern:

  • \w matches one word character, so \won can match Don, Ron, or ton. section 21.2
  • \W matches whitespace and punctuation, making [\w\W]+ a way to match one or more characters of any kind. section 21.4
  • Include both classes when text may contain punctuation such as a hyphen, which \w alone would not match. section 21.4

Searching log and event text

In PowerShell, Select-String uses 10\.0;[\w\W]+\+Gecko to find log lines containing Windows NT 10.0 user-agent text followed by +Gecko. A related event-log pattern, WIN[\W\w]+TM[234][0-9]\$, allows punctuation between the account prefix and suffix. section 21.4

PS C:\logfiles> get-childitem -filter *.log -recurse | 
Select-string -pattern "10\.0;[\w\W]+\+Gecko"
W3SVC1\u_ex120420.log:14:2012-04-20 21:45:04 10.211.55.30 GET /MyApp1/Testpage.asp 
    - 80 - 10.211.55.29 Mozilla/5.0+(Windows+NT+10.0;+WOW64;+rv:11.0)+Gecko/20100101+Firefox/11.0 200 0 0 1125
W3SVC1\u_ex120420.log:15:2012-04-20 21:45:04 10.211.55.30 GET /TestPage.asp 
    - 80 - 10.211.55.29 Mozilla/5.0+(Windows+NT+10.0;+WOW64;+rv:11.0)+Gecko/20100101+Firefox/11.0 200 0 0 1 109

Inspect a Select-String command and matching IIS log output that uses [\w\W]+ to span the user-agent text. section 21.4

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

How deep it goes
Defines \w and \W, demonstrates their combinations, and stops short of regex implementation internals.
Examples
Uses \won, browser user-agent patterns, IIS log searches, and computer-account event filters.
What it assumes
Assumes basic regular-expression syntax and introduces word-character terminology through concrete PowerShell patterns.
What you can do after
Apply it via PowerShell Select-String to locate log or event text containing controlled word and nonword sequences.
How it connects
Connects word characters with regular expressions, character classes, Select-String, IIS logs, and Windows event logs.

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.