Solved

PowerShell REGEX

Posted on 2014-10-08
3
272 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
  • 2
3 Comments
 
LVL 39

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 39

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Synchronize a new Active Directory domain with an existing Office 365 tenant
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will 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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now