简体   繁体   中英

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.

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.

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

I can get the line without quotes around the IP address with this:

[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:

[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:

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

Alternatively, you could use -replace as that is by default case-insensitive

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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