Chapter 13. Regular expressions

 

This chapter covers

  • Regular expression syntax
  • The –match operator
  • Regular expressions in Select-String
  • Regular expressions in the switch statement
  • The REGEX object

Regular expressions are a powerful and complex language you can use to describe data patterns. Most pattern-based data, including email addresses, phone numbers, IP addresses, and more, can be represented as a “regex,” as they’re also known.

We need to set some limits for this chapter, because regular expressions are complex enough to deserve their own book. PowerShell uses industry-standard regex syntax and supports the full .NET regular expression library, but we’ll only be covering the basics. Most of our focus will be on how PowerShell uses a regex once you’ve written one. If you’d like to explore regular expressions further, write more complex expressions, or look for a regex that meets a particular need, we recommend one of these two books as a starting place: Mastering Regular Expressions by Jeffrey E.F. Friedl (a favorite), and Regular Expression Pocket Reference by Tony Stubblebine, both from O’Reilly. You can also visit http://RegExLib.com, a free community repository of regular expressions for specific tasks.

13.1. Basic regular expression syntax

13.2. The –match operator

13.3. The Select-String cmdlet

13.4. The Switch statement

13.5. The REGEX object

13.6. Subexpressions and named captures

13.7. Summary