Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see which options are available in a RegexOptions enum?

How to use this function from powershell script to perform a case insensitive match with using the -cmatch operator:

static System.Text.RegularExpressions.Match Match(
     string input, 
     string pattern,
     System.Text.RegularExpressions.RegexOptions options)

I'm thinking its something like this:

PS>  $input = "one two three"

PS>  $m = [regex]::Match($input, "one", ????)

The part I have a question about is the ???? above.

How do you see what System.Text.RegularExpressions.RegexOptions are available from powershell prompt and what's the syntax to use it in the code above?

like image 581
Bimo Avatar asked Mar 07 '23 02:03

Bimo


2 Answers

The easy "cheat" way to see what options are available in an enum is to use tab completion within the PowerShell prompt or ISE. Start with [System.Text.RegularExpressions.RegexOptions]:: and then use CTRL SPACE (or TAB) to see the options.

The programmatic way, since RegexOptions is an enum, is this:

[System.Enum]::GetNames([System.Text.RegularExpressions.RegexOptions])

When you need to pass enum values in PowerShell, you can pass the long-form fully-qualified enum value (this is my preference):

[System.Text.RegularExpressions.RegexOptions]::IgnoreCase

Or you can pass the numeric value directly (1), or you can pass a string and it will be matched to the enum's text value 'IgnoreCase'.

For your actual question: using [regex]::Match() is already case sensitive. You can just pass [System.Text.RegularExpressions.RegexOptions]::None to your call (or 0, or 'None').

If you want it to be case-insensitive, then you use the IgnoreCase enum value as above.

Because this type of enum is a series of bit flags, you can combine them. To do that you use the -bor (bitwise or, or binary or) operator:

$myOptions = [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor 
        [System.Text.RegularExpressions.RegexOptions]::SingleLine -bor
        [System.Text.RegularExpressions.RegexOptions]::IgnorePatternWhitespace

But PowerShell's convenient coercion of strings to enums doesn't stop at single values. As mklement0 reminded me, you can comma separate the enum names within a string and PowerShell will still correctly parse it.

So you can use a string like 'IgnoreCase, SingleLine, IgnorePatternWhitespace' directly when you need to pass in RegexOptions. You can also pre-cast it:

$myOptions = 'IgnoreCase, SingleLine, IgnorePatternWhitespace' -as [System.Text.RegularExpressions.RegexOptions]
$myOptions = [System.Text.RegularExpressions.RegexOptions]'IgnoreCase, SingleLine, IgnorePatternWhitespace'
like image 153
briantist Avatar answered Mar 10 '23 11:03

briantist


Simplifying braintist answer a little bit to use namespaces:

PS> using namespace System.Text.RegularExpressions

PS> [Enum]::GetNames([RegexOptions])
None
IgnoreCase
Multiline
ExplicitCapture
Compiled
Singleline
IgnorePatternWhitespace
RightToLeft
ECMAScript
CultureInvariant

PS>  $input = "one two three"

PS>  $m = [regex]::Match($input, "one", [RegexOptions]::IgnoreCase)
like image 21
Bimo Avatar answered Mar 10 '23 09:03

Bimo