简体   繁体   English

为什么这个正则表达式在 powershell 中不起作用?

[英]why this regex does not work in powershell?

$pattern = 'trigger(.+?)(\r|\n|\r\n){2}'
Write-Host "PATTERN $pattern"
Write-Host ("- trigger: blah`n blah`n blah blah`n`n not blah" -replace $pattern, "trigger: none")

Output should be "- trigger: none not blah" Output 应该是“- 触发器:没有不是废话”

There are two problems:有两个问题:

  • .+ matches everything except for newlines, use the inline flag (?s) or a modified dot [\S\s]+ .+匹配除换行符以外的所有内容,使用内联标志(?s)或修改后的点[\S\s]+
  • \r\n is the Windows line break and \n *unix; \r\n 是 Windows 换行符和\n *unix; therefore it is best to make \r optional ?因此最好让\r可选? : (\r?\n) : (\r?\n)
$pattern = 'trigger([\s\S]+?)(\r?\n){2}'
Write-Host "PATTERN $pattern"
Write-Host ("- trigger: blah`n blah`n blah blah`n`n not blah" -replace $pattern, "trigger: none")

- trigger: none not blah

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM