简体   繁体   English

如何多次正则表达式PowerShell

[英]How to regex multiple times powershell

I am just learning powershell and cant find how to run differents regex with powershell. 我只是在学习powershell,无法找到如何使用powershell运行不同的正则表达式。

$input_path = 'C:\site-download\input.txt'
$output_file = 'C:\site-download\output.txt'
$regex = '(?<month>VPIP<\/span><span class=""right"">\d{2}.\d{1})'
$regex2 = '(?<month>VPIP<\/span><span class=""right"">\d{2}.\d{1})'
$regex3 = '(?<month>VPIP<\/span><span class=""right"">\d{2}.\d{1})'
$regex... = '(?<month>VPIP<\/span><span class=""right"">\d{2}.\d{1})'

select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches} | % { $_.Value }| 
Foreach-Object {$_ -replace '</span><span class=""right"">', ' = '} > $output_file

$regex works good, but how can i add $regex2 and $regex3 ... to outputfile? $regex效果很好,但是如何将$regex2$regex3 ...添加到outputfile?

Thanks 谢谢

You just need a small change to your last section of your pipeline. 您只需要对管道的最后一部分进行少量更改。 Instead of using > $output_file just pipe the output of the foreach loop to Out-File cmdlet. 而不是使用> $output_file而是将foreach循环的输出通过管道传递到Out-File cmdlet。 So you should be able to have your last line of code look like this: 因此,您应该能够使最后一行代码如下所示:

select-string -Path $input_path -Pattern $regex -AllMatches | 
    % { $_.Matches} | % { $_.Value } |
    Foreach-Object {$_ -replace '</span><span class=""right"">', ' = '} | 
    Out-File $output_file

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

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