英文:
Replace "( with name Tr in powershell text file
问题
如何替换这两个特殊字符,被视为一个 -> "(
为 Tr
。
Get-ChildItem "C:\Users\marich\Desktop\richi\*.sql" -Recurse | ForEach-Object -Process {
(Get-Content $_) -Replace ''''"('''', 'Tr'| Set-Content $_
}
需要帮助。
英文:
How do we replace these two special characters considered as one -> "(
to Tr
.
Get-ChildItem "C:\Users\marich\Desktop\richi\*.sql" -Recurse | ForEach-Object -Process {
(Get-Content $_) -Replace '''"(''', 'Tr'| Set-Content $_
}
kindly need assistance
答案1
得分: 0
使用-replace
,您需要使用\
转义(
,以便按原样匹配它,因为它是正则表达式的特殊字符之一。请参阅转义字符。
Get-ChildItem "C:\Users\marich\Desktop\richi\*.sql" -Recurse | ForEach-Object {
(Get-Content $_ -Raw) -replace '\"\(\' , 'Tr' | Set-Content $_
}
使用.Replace
, 字符串方法会更简单,其中一切都按原样匹配,无需转义:
Get-ChildItem "C:\Users\marich\Desktop\richi\*.sql" -Recurse | ForEach-Object {
(Get-Content $_ -Raw).Replace('\"(' , 'Tr') | Set-Content $_
}
英文:
With -replace
you would need to escape the (
with \
to match it literally as it is a regex special character. See Escaping characters.
Get-ChildItem "C:\Users\marich\Desktop\richi\*.sql" -Recurse | ForEach-Object {
(Get-Content $_ -Raw) -replace '"\(', 'Tr' | Set-Content $_
}
It's much easier with .Replace
, the String method, where everything is matched literally and no escaping is needed:
Get-ChildItem "C:\Users\marich\Desktop\richi\*.sql" -Recurse | ForEach-Object {
(Get-Content $_ -Raw).Replace('"(', 'Tr') | Set-Content $_
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论