PowerShell to remove text from a string
Another way to do this is with operator -replace.
$TestString = "test=keep this, but not this."$NewString = $TestString -replace ".*=" -replace ",.*"
.*=
means any number of characters up to and including an equals sign.
,.*
means a comma followed by any number of characters.
Since you are basically deleting those two parts of the string, you don't have to specify an empty string with which to replace them. You can use multiple -replaces, but just remember that the order is left-to-right.
$a="some text =keep this,but not this"$a.split('=')[1].split(',')[0]
returns
keep this
This should do what you want:
C:\PS> if ('=keep this,' -match '=([^,]*)') { $matches[1] }keep this