[英]Python Multiline Replace
I'm trying to get python to run a PowerShell script over Windows RM, because of the encoding I inject the parameters into the top of the file, however this means I need to remove the existing Param function, I've nearly managed to get正則表達式工作,但有時會弄亂我的正則表達式的“參數(強制)”部分,有人可以幫我解決它。
我不希望它與腳本中其他地方的 Param 一詞相匹配。
......
Param(
[Parameter(Mandatory)]
[String]$TaskName,
[Switch]$Debug = $false
)
# Debug
if ($Debug)
Python 2.7 命令:
args = re.sub("參數(\s*\r*\n[^)]+)", "", scriptfile.read(), re.M)
您可以使用
(?sm)^\s*Param\(.*?\)$
請參閱正則表達式演示。 此模式假定)
右側匹配邊界位於行尾,並且Param
內部行不能以)
字符結尾。
詳情:
(?sm)
- 啟用re.S
和re.M
選項^
- 行開始\s*
零個或多個空格Param\(
- Param(
字符串.*?
- 盡可能少的任何零個或多個字符\)
- a )
字符$
- 行尾請參閱Python 演示:
import re
test_str = "Param(\n [Parameter(Mandatory)]\n [String]$TaskName,\n [Switch]$Debug = $false\n)\n\n# Debug\nif ($Debug)\n\nParam(...)"
print (re.sub(r"^\s*Param\(.*?\)$", '', test_str, 0, re.M|re.S))
Output 是
# Debug
if ($Debug)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.