I am using an application that allows the use of a regex to control the naming of entities.
I have a bunch of entities named like this (each line is the whole name):
Subsystem - CPU - Utilisation [1x]
Subsystem - CPU - Utilisation [2x]
Subsystem - CPU - Utilisation [4x]
Subsystem - CPU - Queue Length
Subsystem - Disk - Space
Subsystem - Disk - Capacity
And I need to use the regex to turn each one into this:
CPU \n Utilisation
CPU \n Queue Length
Disk \n Space
Disk \n Capacity
(the spaces around the \n are for clarity, and not in the real output)
The regex I’m using for that is:
Find: ^Subsystem - (.*) - (.*)( \[.*\])$
Replace: ${1}\n${2}
Which works for the ones with a [something] part, but not those without.
Essentially, I need to get part1 and part2, where part2 is terminated either by [ or the end of the line.
This regex should get you the matches:Tested at this Rubular link
Edit:
Updated to include “Queue Length”
Tested at this Rubular link
Breakdown:
^Subsystem -:self explanatory, matching the first few constant chars([^\s-]+):capturing group of a negative charset, basically that matches anything until it hits a space or a hyphen(.+?)(?=(?:\s+\[)|$):Positive lookahead to match anything (non-greedily) that is always followed by EITHER spaces and “[” OR end of line. The?:is a non-capturing group so that it does not match it..*$:match anything else until end of line