Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

How to make regex case insensitive

Closed Thread
 
Thread Tools Display Modes
Old 11-07-2007   #1 (permalink)
stephenodonoghue
Guest


 

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

Old 11-07-2007   #2 (permalink)
Kiron
Guest


 

Re: How to make regex case insensitive

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

--
Kiron
Old 11-07-2007   #3 (permalink)
stephenodonoghue
Guest


 

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

Old 11-07-2007   #4 (permalink)
Keith Hill [MVP]
Guest


 

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

Old 11-07-2007   #5 (permalink)
Kiron
Guest


 

Re: How to make regex case insensitive

Also...

$sometext -replace "error|warning|fault"



--
Kiron
Old 11-08-2007   #6 (permalink)
Keith Hill [MVP]
Guest


 

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

Old 11-08-2007   #7 (permalink)
stephenodonoghue
Guest


 

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!

Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex IT Staff PowerShell 3 1 Week Ago 07:33 AM
regex help tonyr PowerShell 4 07-02-2008 02:13 PM
Regex Help dm_14 PowerShell 7 06-11-2008 08:33 PM
Regex Help Christopher Robin .NET General 1 03-31-2008 02:02 PM
regex IT Staff PowerShell 4 01-02-2008 02:07 AM








Vistax64.com 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 2005-2008

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 47 48 49 50