使用PowerShell解压缩子串

ach*_*chi 46 string powershell-2.0

如何使用PowerShell提取子字符串?

我有这个字符串......

"-----start-------Hello World------end-------"
Run Code Online (Sandbox Code Playgroud)

我必须提取......

Hello World
Run Code Online (Sandbox Code Playgroud)

最好的方法是什么?

Mat*_*ard 45

-match运营商测试一个正则表达式,用魔法变结合起来$matches,让您的结果

PS C:\> $x = "----start----Hello World----end----"
PS C:\> $x -match "----start----(?<content>.*)----end----"
True
PS C:\> $matches['content']
Hello World
Run Code Online (Sandbox Code Playgroud)

每当对正则表达式有疑问时,请查看此站点:http://www.regular-expressions.info

  • 我不知道在PowerShell中使用正则表达式这是否直截了当!非常感谢!!! (4认同)

小智 37

Substring方法为我们提供了一种基于起始位置和长度从原始字符串中提取特定字符串的方法.如果只提供一个参数,则将其作为起始位置,并输出字符串的其余部分.

PS > "test_string".Substring(0,4)
Test
PS > "test_string".Substring(4)
_stringPS >
Run Code Online (Sandbox Code Playgroud)

链接文字

但这更容易......

 $s = 'Hello World is in here Hello World!'
 $p = 'Hello World'
 $s -match $p
Run Code Online (Sandbox Code Playgroud)

最后,通过一个只选择.txt文件的目录并搜索"Hello World"的出现来:

dir -rec -filter *.txt | Select-String 'Hello World'
Run Code Online (Sandbox Code Playgroud)

  • +1:然而正则表达式捕获开始和结束标记之间的内容会更好,例如"-----开始-------(.+?)------结束------ - "(未经测试的正则表达式,我不是正则表达式大师) (3认同)

mjs*_*squ 13

不确定这是否有效,但PowerShell中的字符串可以使用数组索引语法引用,与Python类似.

它并不完全直观,因为第一个字母被引用index = 0,但它确实:

  • 允许第二个索引号比字符串长,而不会生成错误
  • 反向提取子字符串
  • 从字符串末尾提取子字符串

这里有些例子:

PS > 'Hello World'[0..2]
Run Code Online (Sandbox Code Playgroud)

产生结果(为清晰起见包括索引值 - 未在输出中生成):

H [0]
e [1]
l [2]
Run Code Online (Sandbox Code Playgroud)

通过传递可以使其更有用-join '':

PS > 'Hello World'[0..2] -join ''
Hel
Run Code Online (Sandbox Code Playgroud)

使用不同的索引可以获得一些有趣的效果:

前锋

使用小于第二个的第一个索引值,并且将按照您的预期在前向方向上提取子字符串.这次第二个索引值远远超过字符串长度,但没有错误:

PS > 'Hello World'[3..300] -join ''
lo World
Run Code Online (Sandbox Code Playgroud)

不同于:

PS > 'Hello World'.Substring(3,300)
Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within
the string.
Run Code Online (Sandbox Code Playgroud)

向后

如果提供的第二个索引值低于第一个索引值,则反向返回该字符串:

PS > 'Hello World'[4..0] -join ''
olleH
Run Code Online (Sandbox Code Playgroud)

从结束

如果使用负数,则可以引用字符串末尾的位置.要提取'World',最后5个字母,我们使用:

PS > 'Hello World'[-5..-1] -join ''
World
Run Code Online (Sandbox Code Playgroud)

  • 您也可以使用-join的一元形式:`-join'Hello World'[-5 ..- 1]` (2认同)

Chr*_*udd 6

基于马特的回答,这里有一个跨换行搜索并且很容易修改以供您自己使用

$String="----start----`nHello World`n----end----"
$SearchStart="----start----`n" #Will not be included in results
$SearchEnd="`n----end----" #Will not be included in results
$String -match "(?s)$SearchStart(?<content>.*)$SearchEnd"
$result=$matches['content']
$result
Run Code Online (Sandbox Code Playgroud)

——

注意:如果你想对一个文件运行这个,请记住 Get-Content 返回一个数组而不是单个字符串。您可以通过执行以下操作来解决此问题:

$String=[string]::join("`n", (Get-Content $Filename))
Run Code Online (Sandbox Code Playgroud)


Viv*_*ngh 6

PS> $a = "-----start-------Hello World------end-------"
PS> $a.substring(17, 11)
         or
PS> $a.Substring($a.IndexOf('H'), 11)
Run Code Online (Sandbox Code Playgroud)

$a.Substring(argument1, argument2)->此处argument1=所需字母的起始位置,以及argument2=要输出的子字符串的长度。

这里17是字母表的索引,'H'由于我们要打印到Hello World,因此我们提供11作为第二个参数


Esp*_*o57 5

其他解决方案

$template="-----start-------{Value:This is a test 123}------end-------"
$text="-----start-------Hello World------end-------"

$text | ConvertFrom-String -TemplateContent $template
Run Code Online (Sandbox Code Playgroud)