KeykoYume
KeykoYume

Reputation: 2645

Regular Expression Matching C#

I need to match the following regular expression

lines...
# TYPE word expression
lines...

where expression can be only one of the following summary, counter, gauge, histogram or untyped

For example:

# HELP http_requests A summary of all the requests being made
# TYPE http_requests summary
http_request{requestMethod="GET",requestPath="/status/detailed",requestStatusCode="503"} 824
http_request{requestMethod="GET",requestPath="/status/detailed",requestStatusCode="503"} 334

I tried the following but it seems to not be working

?^{# TYPE}\s\w\s(summary|counter|gauge|histogram|untyped)$?

Any idea what I am missing?

Upvotes: 1

Views: 43

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626691

You may use

(?m)^# TYPE +\w+ +(summary|counter|gauge|histogram|untyped)\r?$

That is what you need to match lines in a multiline string.

Details

  • (?m) - multiline modifier
  • ^ - start of a line
  • # TYPE + - # TYPE substring and one or more spaces
  • \w+ + - 1+ word chars and then 1+ spaces
  • (summary|counter|gauge|histogram|untyped) - one of the substrings in the alternation group
  • \r? - an optional CR symbol (necessary as $ does not match before this symbol in .NET regex)
  • $ - end of a line.

Upvotes: 1

Bogdan Anghel
Bogdan Anghel

Reputation: 184

Successfully tested with: # TYPE (.*)\b(summary|counter|gauge|histogram|untyped)\b\r?\n?

This works if you match against the whole example (multiple lines of text), but if you iterate through every line and match against the regex use:

^# TYPE (.*)\b(summary|counter|gauge|histogram|untyped)\b\r?\n?$

Upvotes: 0

Related Questions