I want to remove certain parts of a certain string in Powershell 7.2.4, how do I do it?

141 Views Asked by At

I have the string "url": "https://maven.fabricmc.net/net/fabricmc/fabric-installer/0.11.0/fabric-installer-0.11.0.jar". I want to remove everything from the string except https://maven.fabricmc.net/net/fabricmc/fabric-installer/0.11.0/fabric-installer-0.11.0.jar (I want to remove the quotes as well).

How do I go about it?

3

There are 3 best solutions below

1
On BEST ANSWER
$str = """url"": ""https://maven.fabricmc.net/net/fabricmc/fabric-installer/0.11.0/fabric-installer-0.11.0.jar"""
$str.Split(""": """)[1].Replace("""","")
0
On

The shortest I could think of Might have the incorrect indexnumber, just test switching '3'

( $str -split """ )[3]
2
On

You could use the -replace operator and extract the section you want.

'"url": "https://maven.fabricmc.net/net/fabricmc/fabric-installer/0.11.0/fabric-installer-0.11.0.jar"' -replace '"url": "(.+)"','$1'

However since this appears to be in JSON format you may be better off using ConvertFrom-Json on the entire thing and then accessing the property through dot notation.

It's slightly easier than trying to regex or string match sections of JSON.

$converted = Get-Content file.json | ConvertFrom-Json
$converted.url