【问题标题】:Powershell Regex matches leading and trailing spacesPowershell Regex 匹配前导和尾随空格
【发布时间】:2014-07-13 15:12:42
【问题描述】:

我想与 Powershell 匹配 - 替换所有未引用的 $test 事件(下面示例中的第 3、4、6、7 行)

1) '$test'
2) test
3) $test
4) foo $test bar
5) foo '$test' bar
6) foo "$test" foo
7) "$test"

我使用了下面的正则表达式

'[^\'']\$test[^\'']'

但这也匹配空格,例如在第 4 行,导致替换修剪它们。

如果没有嵌入单引号,如何准确匹配 $test?

【问题讨论】:

    标签: regex powershell


    【解决方案1】:

    您可以使用下面的正则表达式匹配第 3、4、6、7 行中的 $test(即,$test 不包含在单引号内)

    (?<!\')\$test(?!\')
    

    DEMO

    说明:

    • (?&lt;!\')\$test 负前瞻,确保它不会与单引号后出现的字符串 $test 匹配。
    • (?!\') 下面的字符不会是单引号。仅当两个条件都满足时,正则表达式引擎才匹配字符串 $test。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      • 2016-12-20
      • 2012-02-28
      相关资源
      最近更新 更多