简体   繁体   English

正则表达式用Powershell替换报价

[英]Regex Replace quotes with Powershell

I am editing some web log files and I want to remove the double quotes from some feilds, but not all. 我正在编辑一些Web日志文件,并且想从某些字段中删除双引号,但不是全部。

For example, in the following line, I want to remove the double quotes from the IP address and server.domain.com, but leave the rest. 例如,在下一行中,我想从IP地址和server.domain.com中删除双引号,但其余部分保留。

2013-02-18 21:47:46.636 POST /Path/page.html - - "173.194.79.106" "Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.296)" - "server.domain.com" 200 1079 15

I am looping through the file loading each line with Foreach-Object 我正在遍历文件中使用Foreach-Object加载每一行

I can get the line without quotes around the IP address with this: 我可以使用以下命令在IP地址周围没有引号的行:

[regex]::replace($_,"`"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})`"", '$1')

So I attempted to use the below to do the same to the server.domain.com item with this: 因此,我尝试使用以下方法对server.domain.com项目执行以下操作:

[regex]::replace($_,"`"[a-zA-Z]*\.[dD][oO][mM][aA][iI][nN]\.[cC][oO][mM]`"",'$1')

and my result is this: 我的结果是这样的:

2013-02-18 21:47:46.636 POST /Path/page.html - - "173.194.79.106" "Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.296)" - $1 200 1079 15

what am I doing wrong? 我究竟做错了什么?

Adding brackets and using (?i) for case-insensitivity: 添加括号并使用(?i)区分大小写:

[regex]::replace( $_, "(?i)`"([a-z]*\.domain\.com)`"", '‌​$1' )

Alternatively, you could use -replace as that is by default case-insensitive 或者,您可以使用-replace ,因为默认情况下不区分大小写

$_ -replace "`"([a-z]*\.domain\.com)`"", '$1'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM