简体   繁体   English

正则表达式:忽略特定字符串或跳过/向前看某个括号

[英]Regex : ignoring particular string or skip/look ahead certain bracket

I am using this regex to match and extract information from a log line:我正在使用这个正则表达式来匹配并从日志行中提取信息:

^([^(]+)\(([^)]+)\):\s([\w]+)\s([^:]*):\s(.*)\s\[([^\]]+)\]$

It works as expected for它按预期工作

P:\Application\PativeCommon\Cws2essel.h(50): warning C26812: The enum type 'Cws2essel::eVesselSource' is unscoped. P:\Application\PativeCommon\Cws2essel.h(50):警告 C26812:枚举类型 'Cws2essel::eVesselSource' 没有作用域。 Prefer 'enum class' over 'enum' (Enum.3).更喜欢“枚举类”而不是“枚举”(Enum.3)。 [P:\PativeFunctionLib.vcxproj] [P:\PativeFunctionLib.vcxproj]

But fails due to the (x86) in the following line但由于以下行中的 (x86) 而失败

C:\Program Files (x86)\Microsoft VisualStudio\2021\Community\VC\Tools\MSVC\14.28.29333\include\xmemory(702,82): warning C4244: 'initializing': conversion from 'double' to '_Objty', possible loss of data [G:\agent_work\48\s\Application\FunctionLib.vcxproj] C:\Program Files (x86)\Microsoft VisualStudio\2021\Community\VC\Tools\MSVC\14.28.29333\include\xmemory(702,82):警告 C4244:'initializing':从 'double' 转换为 '_Objty ',可能丢失数据 [G:\agent_work\48\s\Application\FunctionLib.vcxproj]

This is my code:这是我的代码:

let rx = /^([^(]+)\(([^)]+)\):\s([\w]+)\s([^:]*):\s(.*)\s\[([^\]]+)\]/
let [, codeFile, codeLine, severity, ruleId, message, project] = logLine.match(rx);

regex101 Link: https://regex101.com/r/n5kG86/1正则表达式101链接: https://regex101.com/r/n5kG86/1

What modifications do I need to parse the line with (x86) too.我还需要用(x86)解析行进行哪些修改。 Is there a way to ignore this particular string or maybe look ahead and match the () closest to :有没有办法忽略这个特定的字符串,或者向前看并匹配最接近的() :

Please feel free to suggest better/more elegant solutions.请随时提出更好/更优雅的解决方案。

You may use this regex:你可以使用这个正则表达式:

^(.+?)\((\d[\d,]*)\):\s(\w+)\s([^:]*):\s(.*)\s\[([^\]]+)\]$

Updated Regex Demo更新的正则表达式演示

Important changes are in first 2 capture groups:前 2 个捕获组中的重要更改:

  • ^ : Start ^ : 开始
  • (.+?) : First capture group to match a string with 1 or more of any characters (lazy match) (.+?) :第一个捕获组以匹配具有 1 个或多个任意字符的字符串(惰性匹配)
  • \( : Match opening ( \( : 比赛开场(
  • (\d[\d,]*) : 2nd capture group to match a string starting with a digit followed by 0 or more digit/comma characters (\d[\d,]*) :第二个捕获组以匹配以数字开头后跟 0 个或多个数字/逗号字符的字符串
  • \) : Match closing ) \) : 匹配结束)

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

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