简体   繁体   English

PowerShell 从字符串中提取文本

[英]PowerShell Extracting text from a string

I'm really terrible when it comes to Regex and have stuck for a little while now.当谈到正则表达式时,我真的很糟糕,现在已经坚持了一段时间。 How can I get Person A from the following string?如何从以下字符串中获取人 A

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum<br>
Affected: Lorem<br>
Duty Officer: Person A<br>
Affected: Ipsum

Thanks in advance!提前致谢!

You can use the below regex to achieve your results:您可以使用以下正则表达式来实现您的结果:

^Duty Officer: ([\w ]+)$

Explanation of the above regex:上述正则表达式的解释:

^ - Represents the start of the given test String. ^ - 表示给定测试字符串的开始。

([\w ]+) - Represents a capturing Matching any word character( [0-9A-Za-z_] ) along with space character (since the names may contain space) one or more times. ([\w ]+) - 表示捕获匹配任何单词字符( [0-9A-Za-z_] )以及空格字符(因为名称可能包含空格)一次或多次。

$ - Represents the end of the given test string. $ - 表示给定测试字符串的结尾。

You can find the demo of the above regex here.您可以在此处找到上述正则表达式的演示。

POWERSHELL COMMANDS:(You can alter the commands accordingly to suit your needs) POWERSHELL 命令:(您可以相应地更改命令以满足您的需要)

PS C:\Path\To\MyDesktop> $input_path='C:\Path\To\MyDesktop\test.txt'
PS C:\Path\To\MyDesktop> $output_path='C:\Path\To\MyDesktop\testResult.txt'
PS C:\Path\To\MyDesktop> $regex='^Duty Officer: ([\w ]+)$'
PS C:\Path\To\MyDesktop> select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches.groups[1] } | % { $_.Value } > $output_file

The above result takes the input that you provided through test.txt file and prints your desired output in testResult.txt file.上述结果采用您通过test.txt文件提供的输入,并在testResult.txt文件中打印您想要的 output。 Notice in the select-string command to capture only group 1 I used $_.Matches.groups[1] .请注意,在select-string命令中仅捕获组 1 我使用$_.Matches.groups[1]

For better insights of the commands used above;为了更好地了解上面使用的命令; please refer this.请参考这个。

You can get a certain part of text by using indexing.您可以通过使用索引来获取文本的特定部分。 This is more commonly known as the substring function in powershell.这通常称为powershell中的 substring function。 You can also use the split function if you wanted to specify a character to split text with.如果你想指定一个字符来分割文本,你也可以使用分割function。

Example 1示例 1

$string_to_convert = "Duty Officer: Person A"
$string_to_convert.Substring(14,22)
>>> "Person A"

Example 2示例 2

$string_to_convert = "Duty Officer: Person A"
$string_to_convert.Split(" ")[2]
>>> "Person A"

here's yet another method.这是另一种方法。 it presumes the data is ONE multiline string and that there is only the one block of target text involved in that string.它假定数据是一个多行字符串,并且该字符串中只涉及一个目标文本块。

what it does...它能做什么...

  • builds the multiline string to work on构建要处理的多行字符串
  • sets the prefix to split on设置要拆分的前缀
  • sets the suffix to split on设置要拆分的后缀
  • splits on the prefix在前缀上拆分
  • grabs the last item in the resulting array抓取结果数组中的最后一项
  • splits on the suffix在后缀上拆分
  • grabs the 1st item in that array抓取该数组中的第一项
  • trims away any leading or trailing whitespace修剪掉任何前导或尾随空格
  • assigns the value to a $Var将值分配给 $Var
  • displays that value显示该值

the code...编码...

# this presumes the data is ONE multiline string
#     and that there are no other blocks of data in the string
$InStuff = @'
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum<br>
Affected: Lorem<br>
Duty Officer: Person A<br>
Affected: Ipsum
'@

$Prefix = 'Duty Officer:'
$Suffix = '<br>'

$DutyOfficer = (($InStuff -split $Prefix)[-1] -split $Suffix)[0].Trim()

$DutyOfficer

output = Person A output = Person A

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

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