liveBook · Manning

Regular expression

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

A regular expression is a pattern notation for matching and processing text according to specified character sequences.

A regular expression, or regex, is a pattern notation for matching specified text. It is especially useful when filtering or extracting information from larger text files such as web-server logs. section 21.1 section 21.0

Use a regex to recognize a text pattern, not to prove that the data is valid. For example, an IPv4 pattern can recognize 211.193.299.299 even though that address is invalid. section 21.1

Build patterns from literal text and special characters:

  • Literal text matches that text; PowerShell's default matching is case-insensitive, so Car matches car and CAR. section 21.2
  • \w matches a word character, \d a digit, and \s whitespace; uppercase forms such as \W, \D, and \S match the opposites. section 21.2
  • Use . for any single character, brackets such as [aeiou] for a character set, ranges such as [a-z], and [^...] for exclusion. section 21.2
  • Use ?, *, and + to control repetition; parentheses group a subexpression, and backslash escapes a character that normally has special meaning. section 21.2

Test a pattern with -Match

PowerShell's -Match comparison operator returns True or False. Try: "car" -match "c[aeiou]r" returns True; "caaar" -match "c[aeiou]+r" returns True; "cjinr" -match "c[aeiou]+r" returns False. Use -CMatch when a case-sensitive comparison is needed. section 21.3

Search web-server logs

To find HTTP 400–409 errors without matching filenames such as error401.html, search for whitespace around the status code: get-childitem -filter *.log -recurse | select-string -pattern "\s40[0-9]\s" | format-table Filename,LineNumber,Line -wrap. Select-String returns matching lines with file and line information that can be redirected into a report. section 21.4

Regular expressions can also locate browser strings, event-log account names, email addresses, and other recurring text. Start with simple patterns rather than attempting complex expressions immediately; more advanced uses include Switch, input validation, and the automatic $matches collection. section 21.1 section 21.7

Practice by applying patterns to administrative data:

  • Find files whose names contain a two-digit number. section 21.5
  • Filter modules by Microsoft-related properties and display their name, version, author, and company. section 21.5
  • Select installation lines from Windows Update or package-history logs, and find IPv4 addresses in DNS cache or HOSTS-file entries. section 21.5

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

How deep it goes
Explains core syntax and PowerShell operators with runnable examples, but stops short of comprehensive regex complexity.
Examples
Uses character-pattern tests, HTTP error searches, browser-agent searches, event logs, update logs, module data, and IPv4 entries.
What it assumes
Assumes basic PowerShell command use while teaching regex notation and practical text parsing from the beginning.
What you can do after
Apply regexes via PowerShell -Match and Select-String to test patterns and produce filtered administrative reports.
How it connects
Connects regular expressions with text parsing, PowerShell, comparison operators, Select-String, log files, and input validation.

Also explained in

Compare how 27 books explain regular expression →

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.