简体   繁体   English

PowerShell 从字符串中删除文本

[英]PowerShell to remove text from a string

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, =保持这个,

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,但请记住顺序是从左到右。

$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

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 "^.*?=(.*?),.*?$",'$1'

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 的建议,但与该建议不同的是,这不会匹配仅包含等号或逗号的行,它必须按顺序排列。

I referenced @benjamin-hubbard 's answer above to parse the output of dnscmd for A records, and generate a PHP "dictionary"/key-value pairs of IPs and Hostnames.我在上面引用了@benjamin-hubbard 的答案来解析 A 记录的dnscmd输出,并生成 IP 和主机名的 PHP“字典”/键值对。 I strung multiple -replace args together to replace text with nothing or tab to format the data for the PHP file.我将多个-replace args 串在一起以将文本替换为-replace制表符来格式化 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM