PowerShell 从字符串中删除文本 [英] PowerShell to remove text from a string
问题描述
在特定字符之后删除字符串中所有文本的最佳方法是什么?就我而言,="在我的情况下,在另一个字符之后是 ,
,但将文本保留在中间?
样本输入
<块引用>=保留这个,
另一种方法是使用 operator -replace.
$TestString = "test=保留这个,但不保留这个."$NewString = $TestString -replace ".*=" -replace ",.*"
.*=
表示不超过等号的任意数量的字符.
,.*
表示逗号后跟任意数量的字符.
因为您基本上是删除字符串的这两部分,所以您不必指定一个空字符串来替换它们.您可以使用多个 -replaces,但请记住顺序是从左到右.
What is the best way to remove all text in a string after a specific character? In my case "=" and after another character in my case a ,
, but keep the text between?
Sample input
=keep this,
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.
这篇关于PowerShell 从字符串中删除文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!