Reputation: 2848
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. Prefer 'enum class' over 'enum' (Enum.3). [P:\PativeFunctionLib.vcxproj]
But fails due to the (x86) in the following line
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]
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
What modifications do I need to parse the line with (x86)
too.
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.
Upvotes: 1
Views: 52
Reputation: 784948
You may use this regex:
^(.+?)\((\d[\d,]*)\):\s(\w+)\s([^:]*):\s(.*)\s\[([^\]]+)\]$
Important changes are in first 2 capture groups:
^
: Start(.+?)
: First capture group to match a string with 1 or more of any characters (lazy match)\(
: Match opening (
(\d[\d,]*)
: 2nd capture group to match a string starting with a digit followed by 0 or more digit/comma characters\)
: Match closing )
Upvotes: 3