PowerShell 从字符串中删除文本 [英] PowerShell to remove text from a string

查看:118
本文介绍了PowerShell 从字符串中删除文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在特定字符之后删除字符串中所有文本的最佳方法是什么?就我而言,="在我的情况下,在另一个字符之后是 ,,但将文本保留在中间?

样本输入

<块引用>

=保留这个,

解决方案

另一种方法是使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆