-3

I'm using re2 regex to match and negate as in the this link

Following are each possible inputs

  1. abcd-st-ed-qrew
  2. ktm-ewe-abcd-st-ed-qrew
  3. abcd-st-wer-qrew-wabe
  4. ktm-ewe-abcd-st-qrew-qrrt

The conditions are:

  • If string starts with abcd- then, it should negate (shouldn't contain) -ed
  • If string starts with ktm- and also contains -abcd- then, it should negate (shouldn't contain) -ed

The regex should omit for 1st and 2nd inputs since it contains -ed. should pass 3rd and 4th inputs The regex in reference link passed for the match cases of both conditions but not working for negating (not contain) things. Hope need some tweak to negate.

Note: This is for regex on prometheus data in grafana. That's why I'm tagging those too.

James Z
  • 12,209
  • 10
  • 24
  • 44
CdVr
  • 323
  • 3
  • 15
  • How come [this](https://stackoverflow.com/a/63071624/9758194) don't work – JvdV Jul 25 '20 at 11:10
  • Note that I couldn't help you with this yesterday because the expected output you mentioned [in the comments](https://chat.stackoverflow.com/transcript/message/50023128#50023128) didn't make sense and didn't meet the match conditions you mentioned. I posted an answer to this question based on the conditions you provided. Hopefully, the conditions you used here are a more accurate representation of what you need. – 41686d6564 stands w. Palestine Jul 25 '20 at 11:12

1 Answers1

0

You may use the following pattern:

^(?:ktm(?:-e|-(?:[^e\W]|e[^d\W])\w*)*-)?abcd(?:-e|-(?:[^e\W]|e[^d\W])\w*)*$

Demo.

If you want to combine it with the regex from your other question, it would be:

\bkey="((?:ktm(?:-e|-(?:[^e\W]|e[^d\W])\w*)*-)?abcd(?:-e|-(?:[^e\W]|e[^d\W])\w*)*)"

Demo.

The breakdown of the important part (i.e., (?:-e|-(?:[^e\W]|e[^d\W])\w*) is explained in my answer to the other question. That part is used for both abcd and ktm here. Let me know something needs clarification.

  • Just an update. This works. But, since the real implementation is in Prometheus query we need to add escaping character `^(?:ktm(?:-e|-(?:[^e\\W]|e[^d\\W])\\w*)*-)?abcd(?:-e|-(?:[^e]\W]|e[^d\\W])\\w*)*$`. can u please upvote my question too. since, someone downVoted, when i upvote it shows 0. Anyways thanks a lot! – CdVr Jul 30 '20 at 15:48