使用 PowerShell 提取子字符串
如何使用 PowerShell 提取子字符串?
我有这个字符串...
"-----start-------Hello World------end-------"
我必须提取...
Hello World
最好的方法是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
-match
运算符测试正则表达式,将其与神奇变量$matches
结合起来以获得结果每当对正则表达式有疑问时,请查看此站点:http://www.regular-expressions.info
The
-match
operator tests a regex, combine it with the magic variable$matches
to get your resultWhenever in doubt about regex-y things, check out this site: http://www.regular-expressions.info
Substring
方法为我们提供了一种根据起始位置和长度从原始字符串中提取特定字符串的方法。如果仅提供一个参数,则将其视为起始位置,并输出字符串的其余部分。但这更容易......
最后,递归通过目录,仅选择 .txt 文件并搜索“Hello World”的出现:
The
Substring
method provides us a way to extract a particular string from the original string based on a starting position and length. If only one argument is provided, it is taken to be the starting position, and the remainder of the string is outputted.But this is easier...
And finally, to recurse through a directory selecting only the .txt files and searching for occurrence of "Hello World":
不确定这是否有效,但可以使用数组索引语法引用 PowerShell 中的字符串,与 Python 类似。
它不是完全直观,因为第一个字母是由
index = 0
引用的,但它确实:以下是一些示例:
产生结果(为了清晰起见,包括索引值 - 不在输出中生成):
通过传递
-join ' 可以使其更有用'
:通过使用不同的索引,您可以获得一些有趣的效果:
向前
使用小于第二个索引值的第一个索引值,子字符串将在您选择时向前提取会期望。这次第二个索引值远远超过了字符串长度,但没有错误:
与:
向后
不同:如果您提供的第二个索引值小于第一个索引值,则反向返回字符串:
从末尾
如果使用负数,则可以引用从字符串末尾开始的位置。要提取最后 5 个字母
'World'
,我们使用:Not sure if this is efficient or not, but strings in PowerShell can be referred to using array index syntax, in a similar fashion to Python.
It's not completely intuitive because of the fact the first letter is referred to by
index = 0
, but it does:Here are some examples:
Yields the result (index values included for clarity - not generated in output):
Which can be made more useful by passing
-join ''
:There are some interesting effects you can obtain by using different indices:
Forwards
Use a first index value that is less than the second and the substring will be extracted in the forwards direction as you would expect. This time the second index value is far in excess of the string length but there is no error:
Unlike:
Backwards
If you supply a second index value that is lower than the first, the string is returned in reverse:
From End
If you use negative numbers you can refer to a position from the end of the string. To extract
'World'
, the last 5 letters, we use:$a.Substring(argument1, argument2)
-->这里argument1
= 所需字母表的起始位置,argument2
= 您想要作为输出的子字符串的长度。这里 17 是字母表
'H'
的索引,因为我们想要打印直到 Hello World,所以我们提供 11 作为第二个参数$a.Substring(argument1, argument2)
--> Hereargument1
= Starting position of the desired alphabet andargument2
= Length of the substring you want as output.Here 17 is the index of the alphabet
'H'
and since we want to Print till Hello World, we provide 11 as the second argument以马特的答案为基础,这是一个跨换行符搜索的答案,并且很容易修改以供您自己使用
-
注意:如果您想对文件运行此答案,请记住 Get-Content 返回一个数组而不是单个字符串。您可以通过执行以下操作来解决此问题:
Building on Matt's answer, here's one that searches across newlines and is easy to modify for your own use
--
NOTE: if you want to run this against a file keep in mind Get-Content returns an array not a single string. You can work around this by doing the following:
其他解决方案
other solution
由于字符串并不复杂,因此无需添加 RegEx 字符串。简单的搭配就能达到目的
Since the string is not complex, no need to add RegEx strings. A simple match will do the trick
通常,使用 [regex] 直接:
这是
-match
& 的替代方案。$matches
构建于 Matt 的答案这使我们能够在字符串中找到多个匹配结果,但是我们并不真正需要分组在这里提供的功能,因此 look-behind &前瞻断言将允许我们在不捕获组的情况下进行匹配:
PowerShell & .NET 正则表达式与普通正则表达式略有不同,因此请务必引用 快速参考指南以获取其他快捷方式。
Often it is easier to work with [regex] directly:
This is an alternative to
-match
&$matches
building off of Matt's answerThis allows us to find multiple match results in a string but we don't really need that functionality that grouping provides here so look-behind & look-ahead assertions would allow us to match without capturing groups:
PowerShell & .NET regex is slightly different than vanilla regex so be sure to reference the quick reference guide for additional shortcuts.
我需要在日志文件中提取几行,这篇文章有助于解决我的问题,所以我想在此处添加它。如果有人需要提取多行,您可以使用脚本获取与该字符串匹配的单词的索引(我正在搜索“Root”)并提取所有行中的内容。
干杯..!
I needed to extract a few lines in a log file and this post was helpful in solving my issue, so i thought of adding it here. If someone needs to extract muliple lines, you can use the script to get the index of the a word matching that string (i'm searching for "Root") and extract content in all lines.
Cheers..!