Cylindric
Cylindric

Reputation: 5894

Stuck with a regex to exclude optional items

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.

Upvotes: 0

Views: 1642

Answers (5)

codester
codester

Reputation: 117

Just need another * before the $ sign.
Use as below:
^Subsystem - (.*) - (.*)( \[.*\])*$

Upvotes: -1

m0skit0
m0skit0

Reputation: 25873

(Tested on Notepad++)

.+?- +([\w ]+) +- +([\w ]+) .*

Explanation:

.+?- non-greedy until first dash -

+([\w ]+) + - second word (only letters and spaces) match between 1 or more spaces

- +([\w ]+) - second word (only letters and spaces) match preceded by dash and one or more spaces

.* - the rest of the string

Upvotes: 0

Kash
Kash

Reputation: 9029

This regex should get you the matches:

^Subsystem - ([^\s-]+) - ([^\s\[]+)(?=(?:\s\[)|$).*$

Tested at this Rubular link

Edit:
Updated to include "Queue Length"

^Subsystem - ([^\s-]+) - (.+?)(?=(?:\s+\[)|$).*$

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

Upvotes: 3

Oussama Jilal
Oussama Jilal

Reputation: 7739

Then you can just make the [...] part optional :

^Subsystem - (.*) - (.*?)(?: \[.*\])?$

Upvotes: -1

Johannes Fahrenkrug
Johannes Fahrenkrug

Reputation: 44780

Try this (make the last part optional - zero or once):

^Subsystem - (.*) - (.*)( \[.*\])?$

That should work!

Also, I love using http://rubular.com/ to try out my regexps.

Upvotes: 0

Related Questions