【问题标题】:PowerShell Regex get substring between 2 strings [duplicate 2]PowerShell 正则表达式获取 2 个字符串之间的子字符串 [重复 2]
【发布时间】:2022-11-17 02:44:00
【问题描述】:

Old thread

我的问题是:

function GetStringBetweenTwoStrings($firstString, $secondString, $importPath){

    #Get content from file
    $file = Get-Content $importPath

    #Regex pattern to compare two strings
    $pattern = "$firstString(.*?)$secondString"

    #Perform the opperation
    $result = [regex]::Match($file,$pattern).Groups[1].Value

    #Return result
    return $result

}
GetStringBetweenTwoStrings -firstString "Lorem" -secondString "is" -importPath "C:\Temp\test.txt"

这对只有一个 -firstString 和 -secondString 来说很好,但是如何使用此函数按时间顺序在编号的 TXT 中写入多个相同的字符串?

txt - file(with more sections of text):
Lorem
....
is
--> write to 001.txt
Lorem
....
is
--> write to 002.txt
and so forth.... 

并且节的结构得以保留,不在一行中。

我希望有人能告诉我。谢谢。

【问题讨论】:

    标签: string powershell extract


    【解决方案1】:

    您引用的功能有几个限制(我在original answer 上留下了反馈),最值得注意的是只报告过匹配。

    假设一个名为Select-StringBetween的改进函数(见下面的源代码),你可以按如下方式解决你的问题:

    $index = @{ value = 0 }
    Get-ChildItem C:Temp	est.txt |
      Select-StringBetween -Pattern 'Lorem', 'is' -Inclusive |
      Set-Content -LiteralPath { '{0:000}.txt' -f ++$index.Value }
    

    Select-StringBetween源代码:

    注意:语法部分仿照Select-String。定义函数后,运行Select-StringBetween -?查看其语法;希望参数名称是不言自明的。

    function Select-StringBetween {
      [CmdletBinding(DefaultParameterSetName='String')]
      param(
        [Parameter(Mandatory, Position=0)]
        [ValidateCount(2, 2)] 
        [string[]] $Patterns,
        [Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName='File')]
        [Alias('PSPath')]
        [string] $LiteralPath,
        [Parameter(Mandatory, ValueFromPipeline, ParameterSetName='String')]
        [string] $InputObject,
        [switch] $Inclusive,
        [switch] $SimpleMatch,
        [switch] $Trim
      )
      
      process {
    
        if ($LiteralPath) {
          $InputObject = Get-Content -ErrorAction Stop -Raw -LiteralPath $LiteralPath
        }
      
        if ($Inclusive) {
          $regex = '(?s)(?:{0}).*?(?:{1})' -f 
                      ($Patterns[0], [regex]::Escape($Patterns[0]))[$SimpleMatch.IsPresent],
                      ($Patterns[1], [regex]::Escape($Patterns[1]))[$SimpleMatch.IsPresent]
        }
        else {
          $regex = '(?s)(?<={0}).*?(?={1})' -f 
                      ($Patterns[0], [regex]::Escape($Patterns[0]))[$SimpleMatch.IsPresent],
                      ($Patterns[1], [regex]::Escape($Patterns[1]))[$SimpleMatch.IsPresent]
        }
        
        if ($Trim) {
          [regex]::Matches(
            $InputObject,
            $regex
          ).Value.Trim()
        }
        else {
          [regex]::Matches(
            $InputObject,
            $regex
          ).Value
        }
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-01
      • 2014-12-08
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多