Reader small image

You're reading from  Mastering PowerShell Scripting - Fourth Edition

Product typeBook
Published inJun 2021
PublisherPackt
ISBN-139781800206540
Edition4th Edition
Right arrow
Author (1)
Chris Dent
Chris Dent
author image
Chris Dent

Chris Dent is an automation specialist with deep expertise in the PowerShell language. Chris is often found answering questions about PowerShell in both the UK and virtual PowerShell user groups. Chris has been developing in PowerShell since 2007 and has released several modules over the years.
Read more about Chris Dent

Right arrow

Regex options

Regular expression options are used to control the behavior of certain characters in an expression.

Some aspects of the behavior of a regular expression can be modified by placing a regex option either at the beginning or around part of an expression.

In the following example, .+ will only match the first line; the result of the whole match is in the 0 key in the $matches Hashtable:

PS> "First line`nSecond line" -match '.+'
True
PS> $matches[0]
First line

This is because dot (.) will not match line break characters by default. You can change this by setting a single-line option for the expression:

PS> "First line`nSecond line" -match '(?s).+'
True
PS> $matches[0]
First line
Second line

Table 9.7 shows various regex options and their effect:

...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Mastering PowerShell Scripting - Fourth Edition
Published in: Jun 2021Publisher: PacktISBN-13: 9781800206540

Author (1)

author image
Chris Dent

Chris Dent is an automation specialist with deep expertise in the PowerShell language. Chris is often found answering questions about PowerShell in both the UK and virtual PowerShell user groups. Chris has been developing in PowerShell since 2007 and has released several modules over the years.
Read more about Chris Dent

Character

Description