Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PowerShell REGEX

Posted on 2014-10-08
3
Medium Priority
?
299 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 41

Accepted Solution

by:
footech earned 2000 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 41

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
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…

721 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