Matt Demler
Matt Demler

Reputation: 226

Regex to match first part of string (up to the first occurrence of a space character) if it doesn't contain the sequence ;host=

I have this string cpu.usage_system;cpu=cpu-total;host=host1 6.94024205748818 1626401140(graphite metric message with tag support).

I'm trying to match the first part of the string, up to the first occurrence of a space character... but only if that first part of the string doesn't contain ;host=.

I can match all characters up to the first occurrence of a space with ^([\S]+). I have the feeling I should be using a negative lookahead to check for the absence of ;host= but I can't figure out how to put it all together.

The idea is to match the first part of the metric label (& tags), see if contains a host tag, if it does contain a host tag... leave it alone. If it doesn't contain a host tag, append one.

Upvotes: 0

Views: 129

Answers (1)

sj95126
sj95126

Reputation: 6898

This may not be the most elegant solution, depending on what else you need the regex to do, but if you just want to exclude matching lines that contain ;host=, this lookahead should work:

^(?!.*;host=)([\S]+)

Upvotes: 0

Related Questions