【问题标题】:Extract string from multiple line string从多行字符串中提取字符串
【发布时间】:2019-01-03 17:05:22
【问题描述】:

我想提取abc {} 之间的内容。

$s = 'abc {
    123
}'
$s -match 'abc {(.*?)' # true
$s -match 'abc {(.*?)}' # false, expect true

但是,它似乎不做多行匹配?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    当您在SingleLine 模式下执行正则表达式操作时,. 只会匹配换行符。

    您可以使用(?[optionflags]) 添加regex option at the start of your pattern

    $s -match 'abc {(.*?)}'       # $False, `.` doesn't match on newline
    $s -match '(?s)abc {(.*?)}'   # $True, (?s) makes `.` match on newline
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 2011-07-21
      • 2019-10-15
      • 1970-01-01
      相关资源
      最近更新 更多