liveBook · Manning

Regular expression

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

A regular expression is a notation for describing sets of strings and matching text against those descriptions.

A regular expression (regex) describes text to match. In PowerShell automation, use one when a string must follow a rule: remove characters that cannot appear in a URL, or pull a KB identifier from longer page text. Regex is powerful but easy to misuse, so define the allowed or required pattern narrowly and verify the result. section 11.3.3 section 13.3.1

Build a pattern around the rule the automation needs:

  • Use square brackets to create a character list. In a URL-cleanup pattern, the list contains permitted letters, digits, underscores, dashes, single quotes, and periods. section 11.3.3
  • Place ^ inside the character list to negate it: match characters not allowed by the list, then replace each match with an empty string. section 11.3.3
  • Use a literal prefix plus a digit count when an identifier has a known form. KB[0-9]{7} matches KB followed by seven digits. section 13.3.1
  • Keep regex as one tool among splits, substrings, indexes, and replacements; use it where its matching rule makes the task clearer or more practical. section 1.2
[regex]::Replace($string, "[^0-9a-zA-Z_\-'\.]", "")

This replacement removes every character outside the allowed URL set. For example, my-page (name) becomes my-pagename, preserving only characters accepted by the rule. section 11.3.3

Extract KB numbers from scraped update links

After selecting update links with XPath, match each link's InnerText with KB[0-9]{7}. Store the matched value with the full title and parsed update ID in a custom PowerShell object, then test the function against multiple KB numbers. section 13.3.1

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Breaks down character classes and negation for replacement, then applies a fixed KB pattern without developing broader regex syntax.
Examples
Uses URL cleanup for SharePoint site requests and KB-number extraction from Microsoft Update Catalog link text.
What you can do after
Apply it via PowerShell regex methods to remove disallowed URL characters and extract structured KB identifiers.
How it connects
Connects string manipulation, PowerShell regex methods, web scraping, XPath, and Pester testing.

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 Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.