Groups > Microsoft > Windows PowerShell > Re: How to tell if a regex is valid




How to tell if a regex is valid

How to tell if a regex is valid
Tue, 8 Apr 2008 06:43:17 -0700
What is the best way to tell if a string contains a valid regex?

I'm surprised I don't find something like this

[regex]::isvalid($Pattern)

It appears I need to do this.  Is a better way?

if &{trap {$false;continue} $Value -match $Pattern}
Post Reply
Re: How to tell if a regex is valid
Tue, 8 Apr 2008 08:25:48 -0700
On Apr 8, 9:29 pm, Steven Murawski <steven.muraw...@gmail.com> wrote:
> RickB wrote:
> > What is the best way to tell if a string contains a valid regex?
>
> > I'm surprised I don't find something like this
>
> > [regex]::isvalid($Pattern)
>
> > It appears I need to do this.  Is a better way?
>
> > if &{trap {$false;continue} $Value -match $Pattern}
>
> You could use [regex]::ismatch($input, $regex).
>
> Steven Murawski
> Co-Host - Mind of Roothttp://www.mindofroot.com
> Host - PowerShell Basicshttp://powershell-basics.com

RickB,

You don't have to attempt a match to validate the expression; you can
just use the appropriate Regex constructor, which will throw an
ArgumentException if there is a parsing error:

function Regex-Valid( [string] $pattern )
{
    $ErrorActionPreference = "SilentlyContinue"

    $script:valid = $true

    trap {
        $script:valid = $false
    }

    $regex = New-Object Regex $pattern

    $script:valid
}

A function like this might come in handy if you are doing this sort of
thing all the time.

Post Reply
Re: How to tell if a regex is valid
Tue, 8 Apr 2008 08:43:48 -0700
On Apr 8, 9:29 am, Steven Murawski <steven.muraw...@gmail.com> wrote:
> RickB wrote:
> > What is the best way to tell if a string contains a valid regex?
>
> > I'm surprised I don't find something like this
>
> > [regex]::isvalid($Pattern)
>
> > It appears I need to do this.  Is a better way?
>
> > if &{trap {$false;continue} $Value -match $Pattern}
>
> You could use [regex]::ismatch($input, $regex).
>
> Steven Murawski
> Co-Host - Mind of Roothttp://www.mindofroot.com
> Host - PowerShell Basicshttp://powershell-basics.com

Doesn't that throw too?
Post Reply
Re: How to tell if a regex is valid
Tue, 8 Apr 2008 08:53:02 -0700
On Apr 8, 10:25 am, Jeff <jeff.hill...@gmail.com> wrote:
> On Apr 8, 9:29 pm, Steven Murawski <steven.muraw...@gmail.com>
wrote:
>
>
>
>
>
> > RickB wrote:
> > > What is the best way to tell if a string contains a valid regex?
>
> > > I'm surprised I don't find something like this
>
> > > [regex]::isvalid($Pattern)
>
> > > It appears I need to do this.  Is a better way?
>
> > > if &{trap {$false;continue} $Value -match $Pattern}
>
> > You could use [regex]::ismatch($input, $regex).
>
> > Steven Murawski
> > Co-Host - Mind of Roothttp://www.mindofroot.com
> > Host - PowerShell Basicshttp://powershell-basics.com
>
> RickB,
>
> You don't have to attempt a match to validate the expression; you can
> just use the appropriate Regex constructor, which will throw an
> ArgumentException if there is a parsing error:
>
> function Regex-Valid( [string] $pattern )
> {
>     $ErrorActionPreference = "SilentlyContinue"
>
>     $script:valid = $true
>
>     trap {
>         $script:valid = $false
>     }
>
>     $regex = New-Object Regex $pattern
>
>     $script:valid
>
> }
>
> A function like this might come in handy if you are doing this sort of
> thing all the time.
>
> Jeff- Hide quoted text -
>
> - Show quoted text -

I toyed with this too.

Function is-regexp([string]$str)
{trap{$false;continue}if([regex]$str){$true}}

But ultimately I was trying to entirely avoid
catching/trapping and hoped (being pretty new)
there was some trick I'd overlooked.

Post Reply
Re: How to tell if a regex is valid
Tue, 08 Apr 2008 09:29:20 -050
RickB wrote:
> What is the best way to tell if a string contains a valid regex?
> 
> I'm surprised I don't find something like this
> 
> [regex]::isvalid($Pattern)
> 
> It appears I need to do this.  Is a better way?
> 
> if &{trap {$false;continue} $Value -match $Pattern}
> 

You could use [regex]::ismatch($input, $regex).

Steven Murawski
Co-Host - Mind of Root
http://www.mindofroot.com
Host - PowerShell Basics
Post Reply
<< Previous 1 2 Next >>
( Page 1 of 2 )
about | contact