Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - How to make regex case insensitive

Reply
 
Old 11-07-2007   #1 (permalink)
stephenodonoghue


 
 

How to make regex case insensitive

I have the following code in a script I'm writing. The problem I'm
having is it that it's not picking up text which doesn't match the
case used in the regex e.g. 'ERROR' is not being matched whereas
'error' is.

$Pattern = [regex]"error|warning|fault"
$Match = $Pattern.Match($someText)
while ($Match.Success)
{
$someText = $someText.replace($Match.Groups[0].Value, "")
$Match = $Match.NextMatch()
}

The question is how do I modify the above code to make the regex
ignorecase? Is there a flat I can set somewhere?

Thanks


My System SpecsSystem Spec
Old 11-07-2007   #2 (permalink)
Kiron


 
 

Re: How to make regex case insensitive

$Pattern = [regex]"(?i)error|warning|fault"

--
Kiron
My System SpecsSystem Spec
Old 11-07-2007   #3 (permalink)
stephenodonoghue


 
 

Re: How to make regex case insensitive

On Nov 7, 8:44 pm, "Kiron" <Ki...@xxxxxx> wrote:
Quote:

> $Pattern = [regex]"(?i)error|warning|fault"
>
> --
> Kiron
Nice one. Thanks

My System SpecsSystem Spec
Old 11-07-2007   #4 (permalink)
Keith Hill [MVP]


 
 

Re: How to make regex case insensitive

<stephenodonoghue@xxxxxx> wrote in message
news:1194467647.744092.192220@xxxxxx
Quote:

> I have the following code in a script I'm writing. The problem I'm
> having is it that it's not picking up text which doesn't match the
> case used in the regex e.g. 'ERROR' is not being matched whereas
> 'error' is.
>
> $Pattern = [regex]"error|warning|fault"
> $Match = $Pattern.Match($someText)
> while ($Match.Success)
> {
> $someText = $someText.replace($Match.Groups[0].Value, "")
> $Match = $Match.NextMatch()
> }
>
> The question is how do I modify the above code to make the regex
> ignorecase? Is there a flat I can set somewhere?
Another perhaps more straightforward approach to this is:

[regex]::Replace($someText, "(?i)error|warning|fault", "")

--
Keith

My System SpecsSystem Spec
Old 11-07-2007   #5 (permalink)
Kiron


 
 

Re: How to make regex case insensitive

Also...

$sometext -replace "error|warning|fault"



--
Kiron
My System SpecsSystem Spec
Old 11-08-2007   #6 (permalink)
Keith Hill [MVP]


 
 

Re: How to make regex case insensitive

"Kiron" <Kiron@xxxxxx> wrote in message
news:7963BA17-66DD-4DF8-A78E-42A2E0E068D1@xxxxxx
Quote:

> Also...
>
> $sometext -replace "error|warning|fault"
>
>
Yeah, even better. :-)

--
Keith

My System SpecsSystem Spec
Old 11-08-2007   #7 (permalink)
stephenodonoghue


 
 

Re: How to make regex case insensitive


Keith Hill [MVP] wrote:
Quote:

> "Kiron" <Kiron@xxxxxx> wrote in message
> news:7963BA17-66DD-4DF8-A78E-42A2E0E068D1@xxxxxx
Quote:

> > Also...
> >
> > $sometext -replace "error|warning|fault"
> >
> >
>
> Yeah, even better. :-)
>
> --
> Keith
Good points, thanks guys!

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
regex help PowerShell
Regex PowerShell
regex help PowerShell
Regex Help .NET General
regex PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46