模式匹配后需要替换字符串。使用Powershell v4。 日志行是-
"08:02:37.961" level="DEBUG" "Outbound message: [32056][Sent: HTTP]" threadId="40744"
需要完全删除级别和threadId。预期行是-
"08:02:37.961" "Outbound message: [32056][Sent: HTTP]"
已经尝试过跟踪,但是没有用-
$line.Replace('level="\w+"','')
AND
$line.Replace('threadId="\d+"','')
使用正确的替换命令需要帮助。谢谢。
答案 0 :(得分:3)
尝试此正则表达式:
$line = "08:02:37.961" level="DEBUG" "Outbound message: [32056][Sent: HTTP]" threadId="40744"
$line -replace '(\s*(level|threadId)="[^"]+")'
结果:
"08:02:37.961" "Outbound message: [32056][Sent: HTTP]"
正则表达式详细信息:
( # 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 )
答案 1 :(得分:0)
.replace()不使用正则表达式。 https://docs.microsoft.com/en-us/dotnet/api/system.string.replace?view=netframework-4.8 -replace。