string PowerShell 从字符串中删除文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19168475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:06:49  来源:igfitidea点击:

PowerShell to remove text from a string

stringpowershell

提问by JoeRod

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?

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

Sample input

样本输入

=keep this,

=保持这个,

回答by Benjamin Hubbard

Another way to do this is with operator -replace.

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

由于您基本上是删除字符串的这两部分,因此您不必指定一个空字符串来替换它们。您可以使用多个 -replaces,但请记住顺序是从左到右。

回答by Adil Hindistan

$a="some text =keep this,but not this"
$a.split('=')[1].split(',')[0]

returns

回报

keep this

回答by Keith Hill

This should do what you want:

这应该做你想做的:

C:\PS> if ('=keep this,' -match '=([^,]*)') { $matches[1] }
keep this

回答by user208145

I referenced @benjamin-hubbard 's answer above to parse the output of dnscmdfor A records, and generate a PHP "dictionary"/key-value pairs of IPs and Hostnames. I strung multiple -replaceargs together to replace text with nothing or tabto format the data for the PHP file.

我在上面引用了@benjamin-hubbard 的答案来解析dnscmdA 记录的输出,并生成 IP 和主机名的 PHP“字典”/键值对。我将多个-replaceargs串在一起以用空替换文本或tab格式化 PHP 文件的数据。

$DnsDataClean = $DnsData `
    -match "^[a-zA-Z0-9].+\sA\s.+" `
    -replace "172\.30\.","`$P." `
    -replace "\[.*\] " `
    -replace "\s[0-9]+\sA\s","`t"

$DnsDataTable = ( $DnsDataClean | `
    ForEach-Object { 
        $HostName = ($_ -split "\t")[0] ; 
        $IpAddress = ($_ -split "\t")[1] ; 
        "`t`"$IpAddress`"`t=>`t'$HostName', `n" ;
    } | sort ) + "`t`"`$P.255.255`"`t=>`t'None'"

"<?php
`$P = '10.213';
`$IpHostArr = [`n`n$DnsDataTable`n];

?>" | Out-File -Encoding ASCII -FilePath IpHostLookups.php

Get-Content IpHostLookups.php

回答by Nate

This is really old, but I wanted to add my slight variation for anyone else who may stumble across this. Regular expressions are powerful things.

这真的很旧,但我想为其他可能偶然发现的人添加我的细微变化。正则表达式是强大的东西。

To keep the text which falls between the equal sign and the comma:

要保留等号和逗号之间的文本:

-replace "^.*?=(.*?),.*?$",''

This regular expression starts at the beginning of the line, wipes all characters until the first equal sign, captures every character until the next comma, then wipes every character until the end of the line. It then replaces the entire line with the capture group (anything within the parentheses). It will match any line that contains at least one equal sign followed by at least one comma. It is similar to the suggestion by Trix, but unlike that suggestion, this will not match lines which only contain either an equal sign or a comma, it must have both in order.

这个正则表达式从行首开始,擦除所有字符直到第一个等号,捕获每个字符直到下一个逗号,然后擦除每个字符直到行尾。然后用捕获组(括号内的任何内容)替换整行。它将匹配包含至少一个等号后跟至少一个逗号的任何行。它类似于 Trix 的建议,但与该建议不同的是,这不会匹配仅包含等号或逗号的行,它必须按顺序排列。