Solved

PowerShell REGEX

Posted on 2014-10-08
3
292 Views
Last Modified: 2014-10-08
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
Comment
Question by:dbbishop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 40368812
-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
 
LVL 15

Author Closing Comment

by:dbbishop
ID: 40369499
One of there days I'm going to have to learn regex, but with SQL, PowerShell, VB.net, C#, java..., who has time! :-)
0
 
LVL 40

Expert Comment

by:footech
ID: 40369635
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question