【问题标题】:In PowerShell, how do I copy the last alphabet characters from a string which also has numbers in it to create a variable?在 PowerShell 中,如何从包含数字的字符串中复制最后一个字母字符以创建变量?
【发布时间】:2018-01-22 00:36:22
【问题描述】:

例如,如果字符串是 blahblah02baboon - 我需要将“狒狒”与其余部分分开,并且该变量将只计算字符“狒狒”。我需要执行此操作的每个字符串首先具有字母字符,然后是 2 个数字,然后是更多字母字符,因此每次都应该是相同的过程。

任何建议将不胜感激。

【问题讨论】:

  • 请阅读How to Ask 并发布minimal reproducible example。向我们展示您的代码、您尝试了什么、哪里出错了。
  • $text = "blahblah02baboon"; $from = $text.LastIndexOfAny("01234567890".ToCharArray()); $lastWord = $text.Substring($from + 1); Write-Host $lastWord;

标签: powershell alphanumeric


【解决方案1】:

我的建议是学习正则表达式。

'blahblah02baboon' -replace '\D*\d*(\w*)', '$1'

【讨论】:

    【解决方案2】:

    或者使用正则表达式

    $MyString = "01baaab01blah02baboon"
    
    # Match any character which is not a digit
    $Result = [regex]::matches($MyString, "\D+")
    
    # Take the last result
    $LastResult = $Result[$Result.Count-1].Value
    
    # Output
    Write-Output "My last result = $LastResult"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 2020-04-28
      • 2015-11-04
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多