regex help

cmatchett
cmatchett used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT / Software Engineering Consultant
Top Expert 2016
Commented:
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

Author

Commented:
language is powershell

Author

Commented:
$currentvm = 'vm1'

$newvm = 'vm2'

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

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

$String1

this didn't work
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Author

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

Author

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?
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
That last one seems like the best approach to me, probably the lightest weight option.


»bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial