我必须更换文档中的电话号码,但当mu用户没有电话号码时,我必须删除此行 默认文档如下所示:
company
street
T: +00 xxx xxx xxx #phone
F: +00 xxx xxx xxx #Fax
M: +00 xxx xxx xxx #Mobile
例如,当用户没有手机时,我必须删除此行
$content = $content -replace "M:",""
$content = $content -replace "+00",""
$content = $content -replace "xxx xxx xxx",""
但是我收到了这个错误
The regular expression pattern +39 is not valid.
At line:65 char:1
+ $content = $content -replace "+39",""
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (+39:String) [], RuntimeException
+ FullyQualifiedErrorId : InvalidRegularExpression
如果power shell不希望我的字符串
,我现在怎么能删除这一行?删除行:
删除成功后,手机号码文件如下所示:
company
street
T: +00 xxx xxx xxx #phone
F: +00 xxx xxx xxx #Fax
more information
但它看起来应该是这样的
company
street
T: +00 xxx xxx xxx #phone
F: +00 xxx xxx xxx #Fax
more information
答案 0 :(得分:2)
请勿使用正则表达式replace
,而是使用String.Replace
:
$content = $content.Replace("M:","")
$content = $content.Replace("+00","")
$content = $content.Replace("xxx xxx xxx","")
这应该适合你:
$content = Get-Content $filepath | foreach-object { if (!$_.StartsWith("M:")) { $_ } }
$content
答案 1 :(得分:1)
您还可以使用regex
过滤它们:
$content | Where { $_ -notmatch '^M: \+00' }