Home > Back-end >  I want to remove certain parts of a certain string in Powershell 7.2.4, how do I do it?
I want to remove certain parts of a certain string in Powershell 7.2.4, how do I do it?

Time:06-09

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?

CodePudding user response:

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

CodePudding user response:

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

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

CodePudding user response:

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
  • Related