Link to home
Start Free TrialLog in
Avatar of D B
D BFlag for United States of America

asked on

PowerShell REGEX

I need a REGEX that will:
match [one of "CREATE". "Create" or "create"] followed by [one or more spaces], followed by ["PROCEDURE", "Procedure", "procedure", "PROC", "Proc" or "proc"] (note: " and [] are NOT part of the string).

I am wanting to use this in a PowerShell -replace operator, and need assistance with building the regex string, and also have a question.

I am sure this is very easy, but everything I've tried puts the matching of one or more spaces with the last instance of "create" and not after the 'series" of [one of "CREATE", "Create" or "create"]

The source string I want to consider as a match can be any of:
"create procedure ...", "create proc ...", "CREATE    procedure " (several spaces between CREATE and procedure), "CREATE PROC ...", etc.

I want to replace any occurance of a match with "ALTER PROCEDURE ". There will ALWAYS be a space after "PROC" or "PROCEDURE". I don't want to run the risk of replacing "CREATE PROCEDURE" with "ALTER PROCEDUREEDURE" (i.e. finding PROC in PROCEDURE and replacing the "CREATE PROC" portion of the full string.)

My question is: does regex have a precedence, in that it will try from left to right and stop at the first match it finds, such that, if I list PROCEDURE before PROC, it will apply the replace to the full match? Hope that makes sense.
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of D B

ASKER

One of there days I'm going to have to learn regex, but with SQL, PowerShell, VB.net, C#, java..., who has time! :-)
Myself I'd like to learn a bit of SQL, C++ (have dabbled just a bit), and C#.  Luckily it only took me a little time to pick up the basics of regex.  I found http://www.regular-expressions.info/quickstart.html to be a great resource.