Reputation: 81
Need to replace strings after pattern matching. Using powershell v4. Log line is -
"08:02:37.961" level="DEBUG" "Outbound message: [32056][Sent: HTTP]" threadId="40744"
Need to remove level and threadId completely. Expected line is -
"08:02:37.961" "Outbound message: [32056][Sent: HTTP]"
Have already tried following but did not work -
$line.Replace('level="\w+"','')
AND
$line.Replace('threadId="\d+"','')
Help needed with correct replace command. Thanks.
Upvotes: 0
Views: 7080
Reputation: 27428
.replace() doesn't use regex. https://learn.microsoft.com/en-us/dotnet/api/system.string.replace?view=netframework-4.8 -replace does.
Upvotes: 1
Reputation: 61028
Try this regex:
$line = "08:02:37.961" level="DEBUG" "Outbound message: [32056][Sent: HTTP]" threadId="40744"
$line -replace '(\s*(level|threadId)="[^"]+")'
Result:
"08:02:37.961" "Outbound message: [32056][Sent: HTTP]"
Regex details:
( # Match the regular expression below and capture its match into backreference number 1 \s # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) * # Between zero and unlimited times, as many times as possible, giving back as needed (greedy) ( # Match the regular expression below and capture its match into backreference number 2 # Match either the regular expression below (attempting the next alternative only if this one fails) level # Match the characters “level” literally | # Or match regular expression number 2 below (the entire group fails if this one fails to match) threadId # Match the characters “threadId” literally ) =" # Match the characters “="” literally [^"] # Match any character that is NOT a “"” + # Between one and unlimited times, as many times as possible, giving back as needed (greedy) " # Match the character “"” literally )
Upvotes: 5