regex help

Hi Experts,

My old nemesis, regex.  What i want to do is get a vm name out a string and replace it with a new name. When i'm renaming the VM i have to rename all the hard disks etc

e.g.

currentvm = vm1

c:\clusterstorage\vhd\VM1.vhdx
c:\clusterstorage\vhd\VM1-001.vhdx
c:\clusterstorage\vhd\VM1-002.vhdx

newvm = vm2

c:\clusterstorage\vhd\VM2.vhdx
c:\clusterstorage\vhd\VM2-001.vhdx
c:\clusterstorage\vhd\VM2-002.vhdx
cmatchettAsked:
Who is Participating?
 
Bill PrewCommented:
What language?  Do you really need REGEX (feels like overkill for this), seems like a SUBSTITUTE() or REPLACE() might be enough, just looking for "\VM1" and replacing with "\VM2".


»bp
0
 
cmatchettAuthor Commented:
language is powershell
0
 
cmatchettAuthor Commented:
$currentvm = 'vm1'

$newvm = 'vm2'

$String1 = 'c:\clusterstorage\vhd\VM1.vhdx'

$String1.replace("$currentvm","$newvm")

$String1

this didn't work
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
cmatchettAuthor Commented:
It's case sensitive

$currentvm = 'VM1'

$newvm = 'vm2'

$String1 = 'c:\clusterstorage\vhd\VM1.vhdx'

$String1.replace("$currentvm","$newvm")

$String1

c:\clusterstorage\vhd\vm2.vhdx
c:\clusterstorage\vhd\VM1.vhdx

need a solution thats not case sensitive
0
 
cmatchettAuthor Commented:
This is not case sensitive

$currentvm = 'VM1'

$newvm = 'vm2'

$String1 = 'c:\clusterstorage\vhd\VM1.vhdx'

$String1 -replace $currentvm, $newvm

is there a better way?
0
 
Bill PrewCommented:
That last one seems like the best approach to me, probably the lightest weight option.


»bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.