【问题标题】:Powershell: How to grab substring of textPowershell:如何获取文本的子字符串
【发布时间】:2019-09-27 08:43:55
【问题描述】:

我有一个字段,description,其中包含可预测格式的文本,看起来像这两个选项中的一个,

DEACTIVATED on Tue Apr 02 2019

DEACTIVATED on Tue Apr 02 2019 | MANAGER John Smith

我需要在每种情况下只获取日期Tue Apr 02 2019,同时考虑日期之后是否存在文本。

示例用例

$string = "DEACTIVATED on Tue Apr 02 2019 | MANAGER John Smith"
$date   = "Tue Apr 02 2019"

【问题讨论】:

  • 您是否已经尝试过自己解决这个问题?如果是这样,请edit the question 并发布您尝试过的代码

标签: regex powershell substring contains


【解决方案1】:

不确定我是否理解正确,但子字符串的工作方式如下:

$string = "DEACTIVATED on Tue Apr 02 2019 | MANAGER John Smith"
#.substring(starting point, how many)
$date = $string.Substring(15,10)
$date
Tue Apr 02

【讨论】:

    【解决方案2】:

    这是我得到它的方法,因为我必须考虑对象之间的一些潜在差异

    $text = "DEACTIVATED on Tue Apr 02 2019 | MANAGER John Smith"
    $start_pos = $text.IndexOf('on')
    
    $substring = $text.substring($start_pos + 3)
    
    if ($text.IndexOf(' | ') -gt -1) {
        $end_pos = $text.indexOf(' | ')
        $substring = $substring.substring(0, $end_pos)
    }
    

    【讨论】:

      【解决方案3】:

      这里有一种稍微不同的方式来完成这项工作。 [咧嘴一笑]

      它的作用......

      • 第 1 4 行创建一个字符串数组以使用
      • 遍历集合
      • 在“|”上拆分
      • 获取结果数组中的第一项
      • 修剪掉任何前导/尾随空格
      • 使用命名捕获组获取DEACTIVATED on之后的日期字符串
      • 显示生成的命名捕获组
      • 将其转换为[datetime] 对象并显示该对象

      这是代码...

      $DescriptionText = @(
          'DEACTIVATED on Sat May 11 2019'
          'DEACTIVATED on Tue Apr 02 2019 | MANAGER John Smith'
          )
      
      foreach ($DT_Item in $DescriptionText)
          {
          $Null = $DT_Item.Split('|')[0].Trim() -match 'DEACTIVATED on (?<DeactivationDate>.+)'
      
          $Matches.DeactivationDate
          [datetime]::ParseExact($Matches.DeactivationDate, 'ddd MMM dd yyyy', $Null)
          '=' * 20
          }
      

      输出...

      Sat May 11 2019
      
      2019 May 11, Saturday 12:00:00 AM
      ====================
      Tue Apr 02 2019
      2019 April 02, Tuesday 12:00:00 AM
      ====================
      

      我无法弄清楚第一对输出中的空白行来自哪里。 [脸红]

      【讨论】:

        【解决方案4】:

        我会使用:


        ## Q:\Test\2019\05\09\SO_56060672.ps1
        
        $strings = @"
        DEACTIVATED on Tue Apr 02 2019
        
        DEACTIVATED on Tue Apr 09 2019 | MANAGER John Smith
        "@ -split '\r?\n'
        
        $RE = '(?<=DEACTIVATED on ).*\d{4}'
        
        ## Output found datetime strings
        $strings | Select-String $RE | ForEach-Object{$_.Matches.value}
        
        ## if your locale is English convert to [datetime] type
        $strings | Select-String $RE | ForEach-Object{
            [datetime]::ParseExact($_.Matches.value,'ddd MMM dd yyyy',$null)
        }
        
        ## if your locale is NOT English convert to [datetime] type
        $strings | Select-String $RE | ForEach-Object{
            [datetime]::ParseExact($_.Matches.value,'ddd MMM dd yyyy',
               [System.Globalization.CultureInfo]::InvariantCulture)
        }
        

        在我的德语语言环境中输出第一个和最后一个:

        Tue Apr 02 2019
        Tue Apr 09 2019
        
        Dienstag, 2. April 2019 00:00:00
        Dienstag, 9. April 2019 00:00:00
        

        【讨论】:

          猜你喜欢
          • 2011-09-05
          • 2015-08-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-19
          • 1970-01-01
          • 1970-01-01
          • 2013-03-30
          相关资源
          最近更新 更多