查找跨文本的多行并使用 PowerShell 替换 [英] Find multiple lines spanning text and replace using PowerShell

查看:34
本文介绍了查找跨文本的多行并使用 PowerShell 替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用正则表达式搜索来匹配和替换一些文本.文本可以跨越多行(可能有也可能没有换行符).目前我有这个:

I am using a regular expression search to match up and replace some text. The text can span multiple lines (may or may not have line breaks). Currently I have this:

 $regex = "\<\?php eval.*?\>"

Get-ChildItem -exclude *.bak | Where-Object {$_.Attributes -ne "Directory"} |ForEach-Object {
 $text = [string]::Join("`n", (Get-Content $_))
 $text -replace $RegEx ,"REPLACED"}

推荐答案

试试这个:

$regex = New-Object Text.RegularExpressions.Regex "\<\?php eval.*?\>", ('singleline', 'multiline')

Get-ChildItem -exclude *.bak |
  Where-Object {!$_.PsIsContainer} |
  ForEach-Object {
     $text = (Get-Content $_.FullName) -join "`n"
     $regex.Replace($text, "REPLACED")
  }

正则表达式是通过 New-Object 显式创建的,所以可以传入选项.

A regular expression is explicitly created via New-Object so that options can be passed in.

这篇关于查找跨文本的多行并使用 PowerShell 替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆