user3066571
user3066571

Reputation: 1471

PowerShell String replacement with a wildcard

I have a version number in a SQL file that I need to replace via PowerShell. In the file it looks like so:

values
   ('Current', '15.7.1.0'),
   ('New', '15.7.22.0')

I need to replace it with whatever the current version is, but due to the fact that I don't know what the current version number is going to be, nor do I know its exact length (digit count can change as leading zeroes are not used), I need to use wildcards, and what I'm trying isn't working.

Currently this is what I'm trying:

$content = Get-Content C:\Users\user\Desktop\sql\file.sql
$newContent = $content -replace "'Current', '*'", "'Current', '$newVersion'"

And it actually finds it and replaces it, but not all of it for some reason. After running that, what I get is:

values
   ('Current', '16.6.21.0'15.7.1.0'),
   ('New', '15.7.22.0')

So it definitely finds the correct place in the file and does a replace on some of it, but doesn't actually replace the version number. Can anyone tell me what I'm doing wrong?

Upvotes: 2

Views: 1855

Answers (1)

TravisEz13
TravisEz13

Reputation: 2403

As @PetSorAl said, -replace uses wildcards not regular expressions.

Below is an example based on yours.

Example

$content = @"
values
   ('Current', '15.7.1.0'),
   ('New', '15.7.22.0')
"@

$newVersion = '16.6.21.0'
$newContent = $content -replace "'Current', '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'", "'Current', '$newVersion'"
Write-host $newContent

Results

values
   ('Current', '16.6.21.0'),
   ('New', '15.7.22.0')

Upvotes: 2

Related Questions