[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

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.
0
dbbishop
Asked:
dbbishop
  • 2
1 Solution
 
footechCommented:
-replace "create +proc(edure)?","ALTER PROCEDURE"

Open in new window

Regex will try to match as much as it can.  And PowerShell regex is case-insensitive by default.
When using + or *, you can modify it with a ? so that the + or * is non-greedy, meaning it will match a few as possible.
0
 
dbbishopAuthor Commented:
One of there days I'm going to have to learn regex, but with SQL, PowerShell, VB.net, C#, java..., who has time! :-)
0
 
footechCommented:
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.
0

Featured Post

Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now