我正在构建电子邮件,但有时需要删除该电子邮件中的这一部分内容,因此我想使用-replace从#HOUSESTART到#HOUSEEND进行替换,但无法正常工作。
$ body包含此部分以及整个HTML电子邮件的更多内容:
"<p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><b><u><span style='mso-ascii-font-family:Calibri;mso-fareast-font-family:
"Times New Roman";mso-hansi-font-family:Calibri;mso-bidi-font-family:Calibri'>#HOUSESTART<o:p></o:p></span></u></b></p>
<p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><b><u><span style='mso-ascii-font-family:Calibri;mso-fareast-font-family:
"Times New Roman";mso-hansi-font-family:Calibri;mso-bidi-font-family:Calibri'>PLEASE
NOTE</span></u></b><span style='mso-ascii-font-family:Calibri;mso-fareast-font-family:
"Times New Roman";mso-hansi-font-family:Calibri;mso-bidi-font-family:Calibri'>:
As a house manager, you have two email addresses. Your secondary email
address is #EMAIL. The only place you will need to use this email address
is when you are enrolling any device in the Targeted Threat Protection.<span
style='mso-spacerun:yes'> </span><b><span style='background:yellow;mso-highlight:
yellow'>(Only used in manager welcome emails.)</span><o:p></o:p></b></span></p>
<p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><b><span style='mso-ascii-font-family:Calibri;mso-fareast-font-family:
"Times New Roman";mso-hansi-font-family:Calibri;mso-bidi-font-family:Calibri'>#HOUSEEND</span></b><span
style='mso-ascii-font-family:Calibri;mso-fareast-font-family:"Times New Roman";
mso-hansi-font-family:Calibri;mso-bidi-font-family:Calibri'><o:p></o:p></span></p>
我正在使用此命令尝试删除#HOUSESTART和#HOUSEEND之间的所有内容,但并未删除它。
$body = $body -replace "#HOUSESTART.*#HOUSEEND"," "
任何帮助将不胜感激。
默认情况下,.
.NET正则表达式中的元字符匹配除换行符之外的任何字符。
因此,如果要跨多行.*
匹配(即也要匹配换行符),则必须使用内联regex选项 s
((?s)
在regex的开头):
$body = $body -replace '(?s)#HOUSESTART.*#HOUSEEND', ' '
注:
*我使用'...'
(单引号,即逐字字符串),而不是"..."
(可扩展(插值)的字符串),以避免什么之间的PowerShell可以解释混淆了前面,什么正则表达式引擎会看到。
*贪婪地.*
匹配,以便匹配输入的最后一个实例的所有内容;如果可以有多个实例,并且您只想通过下一个实例进行匹配,请使用非贪婪#HOUSEEND
.*?
请注意,此字符串$body
必须是单行,多行字符串才能起作用。
例如,如果您使用类似$body = Get-Content file.txt
set的方法$body
,则会得到一个字符串数组,每个字符串都将-replace
应用于该操作,但将无法使用。在这种情况下,请使用-Raw
开关确保将文件读取为单行多行字符串:$body = Get-Content -Raw file.txt
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句