CINXE.COM

Regular Expressions with PowerShell

<!DOCTYPE html> <html lang="en"><head><meta charset="utf-8"><link rel=canonical href='https://https://www.regular-expressions.info//powershell.html'><title>Regular Expressions with PowerShell</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="author" content="Jan Goyvaerts"> <meta name="description" content="Use regular expressions with PowerShell’s built-in -match and -replace operators, or use the .NET System.Text.RegularExpressions.Regex class"> <meta name="keywords" content=""> <link rel=stylesheet href="regex.css" type="text/css"><script src="theme.js" type="text/javascript"></script><link rel="alternate" type="application/rss+xml" title="New at Regular-Expressions.info" href="updates.xml"> </head> <body bgcolor=white text=black> <div id=top></div> <div id=btntop><div id=btngrid><a href="quickstart.html" target="_top"><div>Quick&nbsp;Start</div></a><a href="tutorial.html" target="_top"><div>Tutorial</div></a><a href="tools.html" target="_top"><div>Tools&nbsp;&amp;&nbsp;Languages</div></a><a href="examples.html" target="_top"><div>Examples</div></a><a href="refflavors.html" target="_top"><div>Reference</div></a><a href="books.html" target="_top"><div>Book&nbsp;Reviews</div></a></div></div> <div id=contents><div id=side> <TABLE CLASS=side CELLSPACING=0 CELLPADDING=4><TR><TD CLASS=sideheader>Regex Tools</TD></TR><TR><TD><A HREF="grep.html" TARGET=_top>grep</A></TD></TR><TR><TD><A HREF="powergrep.html" TARGET=_top>PowerGREP</A></TD></TR><TR><TD><A HREF="regexbuddy.html" TARGET=_top>RegexBuddy</A></TD></TR><TR><TD><A HREF="regexmagic.html" TARGET=_top>RegexMagic</A></TD></TR> </TABLE><TABLE CLASS=side CELLSPACING=0 CELLPADDING=4><TR><TD CLASS=sideheader>General Applications</TD></TR><TR><TD><A HREF="editpadlite.html" TARGET=_top>EditPad Lite</A></TD></TR><TR><TD><A HREF="editpadpro.html" TARGET=_top>EditPad Pro</A></TD></TR> </TABLE><TABLE CLASS=side CELLSPACING=0 CELLPADDING=4><TR><TD CLASS=sideheader>Languages &amp; Libraries</TD></TR><TR><TD><A HREF="boost.html" TARGET=_top>Boost</A></TD></TR><TR><TD><A HREF="delphi.html" TARGET=_top>Delphi</A></TD></TR><TR><TD><A HREF="gnu.html" TARGET=_top>GNU (Linux)</A></TD></TR><TR><TD><A HREF="groovy.html" TARGET=_top>Groovy</A></TD></TR><TR><TD><A HREF="java.html" TARGET=_top>Java</A></TD></TR><TR><TD><A HREF="javascript.html" TARGET=_top>JavaScript</A></TD></TR><TR><TD><A HREF="dotnet.html" TARGET=_top>.NET</A></TD></TR><TR><TD><A HREF="pcre.html" TARGET=_top>PCRE (C/C++)</A></TD></TR><TR><TD><A HREF="pcre2.html" TARGET=_top>PCRE2 (C/C++)</A></TD></TR><TR><TD><A HREF="perl.html" TARGET=_top>Perl</A></TD></TR><TR><TD><A HREF="php.html" TARGET=_top>PHP</A></TD></TR><TR><TD><A HREF="posix.html" TARGET=_top>POSIX</A></TD></TR><TR><TD><A HREF="powershell.html" TARGET=_top>PowerShell</A></TD></TR><TR><TD><A HREF="python.html" TARGET=_top>Python</A></TD></TR><TR><TD><A HREF="rlanguage.html" TARGET=_top>R</A></TD></TR><TR><TD><A HREF="ruby.html" TARGET=_top>Ruby</A></TD></TR><TR><TD><A HREF="stdregex.html" TARGET=_top>std::regex</A></TD></TR><TR><TD><A HREF="tcl.html" TARGET=_top>Tcl</A></TD></TR><TR><TD><A HREF="vbscript.html" TARGET=_top>VBScript</A></TD></TR><TR><TD><A HREF="vb.html" TARGET=_top>Visual Basic 6</A></TD></TR><TR><TD><A HREF="wxwidgets.html" TARGET=_top>wxWidgets</A></TD></TR><TR><TD><A HREF="xml.html" TARGET=_top>XML Schema</A></TD></TR><TR><TD><A HREF="realbasic.html" TARGET=_top>Xojo</A></TD></TR><TR><TD><A HREF="xpath.html" TARGET=_top>XQuery &amp; XPath</A></TD></TR><TR><TD><A HREF="xregexp.html" TARGET=_top>XRegExp</A></TD></TR> </TABLE><TABLE CLASS=side CELLSPACING=0 CELLPADDING=4><TR><TD CLASS=sideheader>Databases</TD></TR><TR><TD><A HREF="mysql.html" TARGET=_top>MySQL</A></TD></TR><TR><TD><A HREF="oracle.html" TARGET=_top>Oracle</A></TD></TR><TR><TD><A HREF="postgresql.html" TARGET=_top>PostgreSQL</A></TD></TR> </TABLE><TABLE CLASS=side CELLSPACING=0 CELLPADDING=4><TR><TD CLASS=sideheader>More on This Site</TD></TR><TR><TD><A HREF="index.html" TARGET=_top>Introduction</A></TD></TR><TR><TD><A HREF="quickstart.html" TARGET=_top>Regular Expressions Quick Start</A></TD></TR><TR><TD><A HREF="tutorial.html" TARGET=_top>Regular Expressions Tutorial</A></TD></TR><TR><TD><A HREF="replacetutorial.html" TARGET=_top>Replacement Strings Tutorial</A></TD></TR><TR><TD><A HREF="tools.html" TARGET=_top>Applications and Languages</A></TD></TR><TR><TD><A HREF="examples.html" TARGET=_top>Regular Expressions Examples</A></TD></TR><TR><TD><A HREF="refflavors.html" TARGET=_top>Regular Expressions Reference</A></TD></TR><TR><TD><A HREF="refreplace.html" TARGET=_top>Replacement Strings Reference</A></TD></TR><TR><TD><A HREF="books.html" TARGET=_top>Book Reviews</A></TD></TR><TR><TD><A HREF="print.html" TARGET=_top>Printable PDF</A></TD></TR><TR><TD><A HREF="about.html" TARGET=_top>About This Site</A></TD></TR><TR><TD><A HREF="updates.html" TARGET=_top>RSS Feed &amp; Blog</A></TD></TR></TABLE></DIV><div class=bodytext><div class=topad><A HREF="https://www.regexbuddy.com/powershell.html" TARGET="_top"><picture><source media="(max-width: 370px)" srcset="ads/320/rxbpowershell100.png 1x, ads/320/rxbpowershell150.png 1.5x, ads/320/rxbpowershell200.png 2x, ads/320/rxbpowershell250.png 2.5x, ads/320/rxbpowershell300.png 3x, ads/320/rxbpowershell350.png 3.5x, ads/320/rxbpowershell400.png 4x"><source media="(max-width: 500px)" srcset="ads/360/rxbpowershell100.png 1x, ads/360/rxbpowershell150.png 1.5x, ads/360/rxbpowershell200.png 2x, ads/360/rxbpowershell250.png 2.5x, ads/360/rxbpowershell300.png 3x, ads/360/rxbpowershell350.png 3.5x, ads/360/rxbpowershell400.png 4x"><source media="(max-width: 660px)" srcset="ads/480/rxbpowershell100.png 1x, ads/480/rxbpowershell150.png 1.5x, ads/480/rxbpowershell200.png 2x, ads/480/rxbpowershell250.png 2.5x, ads/480/rxbpowershell300.png 3x, ads/480/rxbpowershell350.png 3.5x, ads/480/rxbpowershell400.png 4x"><source media="(max-width: 747px)" srcset="ads/640/rxbpowershell100.png 1x, ads/640/rxbpowershell150.png 1.5x, ads/640/rxbpowershell200.png 2x, ads/640/rxbpowershell250.png 2.5x, ads/640/rxbpowershell300.png 3x, ads/640/rxbpowershell350.png 3.5x, ads/640/rxbpowershell400.png 4x"><img src="ads/728/rxbpowershell100.png" srcset="ads/728/rxbpowershell100.png 1x, ads/728/rxbpowershell125.png 1.25x, ads/728/rxbpowershell150.png 1.5x, ads/728/rxbpowershell175.png 1.75x, ads/728/rxbpowershell200.png 2x, ads/728/rxbpowershell250.png 2.5x, ads/728/rxbpowershell300.png 3x, ads/728/rxbpowershell350.png 3.5x, ads/728/rxbpowershell400.png 4x" alt="RegexBuddy—The best regex editor and tester for PowerShell developers!"></picture></A></div> <div class=bulb><h1>Regular Expressions with PowerShell</h1><script type="text/javascript">showbulb();</script></div> <p>PowerShell is a programming language from Microsoft that is primarily designed for system administration. PowerShell is built on top of .NET so <A HREF="dotnet.html" TARGET="_top">.NET’s excellent regular expression support</A> is also available to PowerShell programmers.</p> <p>The discussion below applies equally to Windows PowerShell 1.0 to 5.1, PowerShell Core 6, and PowerShell 7. There haven’t been any changes to the regex syntax in .NET or in .NET Core since .NET 2.0. PowerShell 1.0 was released after .NET 2.0. So all versions of PowerShell use the same regex syntax. Windows PowerShell 2.0 and 5.0 added some features that make it easier to split strings and invoke other Regex() constructors. Other than that, there are no differences between any of the PowerShell versions regarding the use of regular expressions.</p> <h2>PowerShell -Match and -Replace Operators</h2> <p>With the <tt>-match</tt> operator, you can quickly check if a regular expression matches part of a string. E.g. <tt>&apos;test&apos; -match &apos;\w&apos;</tt> returns true, because <TT CLASS=syntax><SPAN CLASS="regexspecial">\w</SPAN></TT> matches <tt class=match>t</tt> in <tt class=string>test</tt>.</p> <p>As a side effect, the <tt>-match</tt> operator sets a special variable called <tt>$matches</tt>. This is an associative array that holds the overall regex match and all capturing group matches. <tt>$matches[0]</tt> gives you the overall regex match, <tt>$matches[1]</tt> the first capturing group, and <tt>$matches[&apos;name&apos;]</tt> the text matched by the named group “name”.</p> <p>The <tt>-replace</tt> operator uses a regular expression to search-and-replace through a string. E.g. <tt>&apos;test&apos; -replace &apos;\w&apos;, &apos;$&$&&apos;</tt> returns <tt>&apos;tteesstt&apos;</tt>. The regex <TT CLASS=syntax><SPAN CLASS="regexspecial">\w</SPAN></TT> matches one letter. The replacement text re-inserts the regex match twice using <tt>$&</tt>. The replacement text parameter must be specified, and the regex and replacement must be separated by a comma. If you want to replace the regex matches with nothing, pass an empty string as the replacement.</p> <p>Traditionally, regular expressions are case sensitive by default. This is true for the .NET framework too. However, it is not true in PowerShell. <tt>-match</tt> and <tt>-replace</tt> are case insensitive, as are <tt>-imatch</tt> and <tt>-ireplace</tt>. For case sensitive matching, use <tt>-cmatch</tt> and <tt>-creplace</tt>. I recommend that you always use the “i” or “c” prefix to avoid confusion regarding case sensitivity.</p> <p>The operators do not provide a way to pass options from .NET’s RegexOptions enumeration. Instead, use <A HREF="modifiers.html" TARGET="_top">mode modifiers</A> in the regular expression. E.g. <TT CLASS=syntax><SPAN CLASS="regexmeta">(?</SPAN><SPAN CLASS="regexmeta">m</SPAN><SPAN CLASS="regexmeta">)</SPAN><SPAN CLASS="regexspecial">^</SPAN><SPAN CLASS="regexplain">test</SPAN><SPAN CLASS="regexspecial">$</SPAN></TT> is the same as using <TT CLASS=syntax><SPAN CLASS="regexspecial">^</SPAN><SPAN CLASS="regexplain">test</SPAN><SPAN CLASS="regexspecial">$</SPAN></TT> with RegexOptions.MultiLine passed to the Regex() constructor. Mode modifiers take precedence over options set externally to the regex. <tt>-cmatch &apos;(?i)test&apos;</tt> is case insensitive, while <tt>-imatch &apos;(?-i)test&apos;</tt> is case sensitive. The mode modifier overrides the case insensitivity preference of the <tt>-match</tt> operator.</p> <h2>PowerShell -Split Operator</h2> <p>PowerShell 2.0, introduced with Windows 7 SP1, added the <tt>-split</tt> operator. It allows you to split a string along regex matches. E.g. <tt>$subject -split &quot;\W+&quot;</tt> splits the subject string along non-word characters, thus returning an array of all the words in the string.</p> <h2>Replacement Text as a Literal String</h2> <p>The <tt>-replace</tt> operator supports the same replacement text placeholders as the Regex.Replace() function in .NET. <TT CLASS=syntax><SPAN CLASS="regexspecial">$&amp;</SPAN></TT> is the overall regex match, <TT CLASS=syntax><SPAN CLASS="regexspecial">$1</SPAN></TT> is the text matched by the first capturing group, and <TT CLASS=syntax><SPAN CLASS="regexspecial">${name}</SPAN></TT> is the text matched by the named group “name”.</p> <p>But with PowerShell, there’s an extra caveat: double-quoted strings use the dollar syntax for variable interpolation. Variable interpolation is done before the Regex.Replace() function (which <tt>-replace</tt> uses internally) parses the replacement text. Unlike Perl, <TT CLASS=syntax><SPAN CLASS="regexspecial">$1</SPAN></TT> is not a magical variable in PowerShell. That syntax only works in the replacement text. The <tt>-replace</tt> operator does not set the <tt>$matches</tt> variable either. The effect is that <tt>&apos;test&apos; -replace &apos;(\w)(\w)&apos;, &quot;$2$1&quot;</tt> (double-quoted replacement) returns the empty string (assuming you did not set the variables $1 and $2 in preceding PowerShell code). Due to variable interpolation, the Replace() function never sees <tt class=string>$2$1</tt>. To allow the Replace() function to substitute its placeholders, use <tt>&apos;test&apos; -replace &apos;(\w)(\w)&apos;, &apos;$2$1&apos;</tt> (single-quoted replacement) or <tt>&apos;test&apos; -replace &apos;(\w)(\w)&apos;, &quot;&#x60;$2&#x60;$1&quot;</tt> (dollars escaped with backticks) to make sure <TT CLASS=syntax><SPAN CLASS="regexspecial">$2</SPAN><SPAN CLASS="regexspecial">$1</SPAN></TT> is passed literally to Regex.Replace().</p> <h2>Using The System.Text.RegularExpressions.Regex Class</h2> <p>To use all of .NET’s regex processing functionality with PowerShell, create a regular expression object by instantiating the System.Text.RegularExpressions.Regex class. PowerShell provides a handy shortcut if you want to use the Regex() constructor that takes a string with your regular expression as the only parameter. <tt>$regex = [regex] &apos;\W+&apos;</tt> compiles the regular expression <TT CLASS=syntax><SPAN CLASS="regexspecial">\W</SPAN><SPAN CLASS="regexspecial">+</SPAN></TT> (which matches one or more non-word characters) and stores the result in the variable <tt>$regex</tt>. You can now call all the methods of the Regex class on your <tt>$regex</tt> object.</p> <p>In PowerShell 5.0 and later you can invoke another Regex() constructor on the class name:</p> <pre>using namespace System.Text.RegularExpressions $regex = [Regex]::new(&apos;^test$&apos;, [RegexOptions]::MultiLine)</pre> <p>In older versions of PowerShell, you have to resort to PowerShell’s <tt>new-object</tt> cmdlet. To set the flag RegexOptions.MultiLine, for example, you’d need this line of code:</p> <pre>$regex = new-object System.Text.RegularExpressions.Regex (&apos;^test$&apos;, [System.Text.RegularExpressions.RegexOptions]::MultiLine)</pre> <p>In any version of PowerShell, <A HREF="modifiers.html" TARGET="_top">mode modifiers</A> inside the regex can provide a shorter solution:</p> <pre>$regex = [regex] &apos;(?m)^test$&apos;</pre> <p>Mode modifiers also work with the <tt>-match</tt>, <tt>-replace</tt>, and <tt>-split</tt> operators.</p><div id=cntmobi><p>|&ensp;<a href='quickstart.html'>Quick&nbsp;Start</a>&ensp;|&ensp;<a href='tutorial.html'>Tutorial</a>&ensp;|&ensp;<a href='tools.html'>Tools&nbsp;&amp;&nbsp;Languages</a>&ensp;|&ensp;<a href='examples.html'>Examples</a>&ensp;|&ensp;<a href='refflavors.html'>Reference</a>&ensp;|&ensp;<a href='books.html'>Book&nbsp;Reviews</a>&ensp;|</p><p>|&ensp;<a href='grep.html'>grep</a>&ensp;|&ensp;<a href='powergrep.html'>PowerGREP</a>&ensp;|&ensp;<a href='regexbuddy.html'>RegexBuddy</a>&ensp;|&ensp;<a href='regexmagic.html'>RegexMagic</a>&ensp;|</p><p>|&ensp;<a href='editpadlite.html'>EditPad Lite</a>&ensp;|&ensp;<a href='editpadpro.html'>EditPad Pro</a>&ensp;|</p><p>|&ensp;<a href='boost.html'>Boost</a>&ensp;|&ensp;<a href='delphi.html'>Delphi</a>&ensp;|&ensp;<a href='gnu.html'>GNU (Linux)</a>&ensp;|&ensp;<a href='groovy.html'>Groovy</a>&ensp;|&ensp;<a href='java.html'>Java</a>&ensp;|&ensp;<a href='javascript.html'>JavaScript</a>&ensp;|&ensp;<a href='dotnet.html'>.NET</a>&ensp;|&ensp;<a href='pcre.html'>PCRE (C/C++)</a>&ensp;|&ensp;<a href='pcre2.html'>PCRE2 (C/C++)</a>&ensp;|&ensp;<a href='perl.html'>Perl</a>&ensp;|&ensp;<a href='php.html'>PHP</a>&ensp;|&ensp;<a href='posix.html'>POSIX</a>&ensp;|&ensp;<a href='powershell.html'>PowerShell</a>&ensp;|&ensp;<a href='python.html'>Python</a>&ensp;|&ensp;<a href='rlanguage.html'>R</a>&ensp;|&ensp;<a href='ruby.html'>Ruby</a>&ensp;|&ensp;<a href='stdregex.html'>std::regex</a>&ensp;|&ensp;<a href='tcl.html'>Tcl</a>&ensp;|&ensp;<a href='vbscript.html'>VBScript</a>&ensp;|&ensp;<a href='vb.html'>Visual Basic 6</a>&ensp;|&ensp;<a href='wxwidgets.html'>wxWidgets</a>&ensp;|&ensp;<a href='xml.html'>XML Schema</a>&ensp;|&ensp;<a href='realbasic.html'>Xojo</a>&ensp;|&ensp;<a href='xpath.html'>XQuery &amp; XPath</a>&ensp;|&ensp;<a href='xregexp.html'>XRegExp</a>&ensp;|</p><p>|&ensp;<a href='mysql.html'>MySQL</a>&ensp;|&ensp;<a href='oracle.html'>Oracle</a>&ensp;|&ensp;<a href='postgresql.html'>PostgreSQL</a>&ensp;|</p></div> <div id=copyright> <P CLASS=copyright>Page URL: <A HREF="https://www.regular-expressions.info/powershell.html" TARGET="_top">https://www.regular-expressions.info/powershell.html</A><BR> Page last updated: 5 November 2021<BR> Site last updated: 06 November 2024<BR> Copyright &copy; 2003-2024 Jan Goyvaerts. All rights reserved.</P> </div> </div> </div> </body></html>

Pages: 1 2 3 4 5 6 7 8 9 10