0

I have a regex which takes the value from the given key as below

Regex .*key="([^"]*)".* InputValue key="abcd-qwer-qaa-xyz-vwxc"

output abcd-qwer-qaa-xyz-vwxc

But, on top of this i need to validate the value with starting only with abcd- and somewhere the following pattern matches -xyz

Thus, the input and outputs has to be as follows:

output for matched

I tried below which is not working as expected

.*key="([^"]*)"?(/Babcd|-xyz).*

The key value pair is part of the large string as below:

object{one="ab-vwxc",two="value1",key="abcd-eest-wd-xyz-bnn",four="obsolete Values"}

I think by matching the key its taking the value and that's y i used this .*key="([^"]*)".*

Note: Its a dashboard. you can refer this link and search for Regex: /"([^"]+)"/ This regex is applied on the query result which is a string i referred. Its working with that regex .*key="([^"]*)".* above. I'm trying to alter with that regexGroup itself. Hope this helps?

Can anyone guide or suggest me on this please? That would be helpful. Thanks!

CdVr
  • 323
  • 3
  • 15
  • 2
    Off-topic, but you should revisit your previously asked questions and see if you can upvote/accept any of the give answers. Its simple courtesy and how this site [works](https://stackoverflow.com/tour). – JvdV Jul 15 '20 at 07:57
  • Further to @JvdV's comment, the link given contains the words, "Accepting doesn't mean it's the best answer, it just means that it worked for the person who asked.". That may be the policy, but I think in practice askers tend to select the answer they find most helpful (provided at least one was helpful). Though selecting an answer is encouraged, you may wish to wait awhile before doing so, mainly because quick selections can discourage some readers from posting additional answers. – Cary Swoveland Jul 15 '20 at 17:11

3 Answers3

3

Looks like you could do with:

\bkey="(abcd(?=.*-xyz\b)(?:-[a-z]+){4})"

See the demo online

  • \bkey=" - A word-boundary and literally match 'key="'
  • ( - Open 1st capture group.
    • abcd - Literally match 'abcd'.
    • (?=.*-xyz\b) - Positive lookahead for zero or more characters (but newline) followed by literally '-xyz' and a word-boundary.
    • (?: - Open non-capturing group.
      • -[a-z]+ - Match an hyphen followed by at least a single lowercase letter.
      • ){4} - Close non-capture group and match it 4 times.
    • ) - Close 1st capture group.
  • " - Match a literal double quote.

I'm not a 100% sure you'd only want to allow for lowercase letter so you can adjust that part if need be. The whole pattern validates the inputvalue whereas you could use capture group one to grab you key.


enter image description here


Update after edited question with new information:

Prometheus uses the RE2 engine in all regular expressions. Therefor the above suggestion won't work due to the lookarounds. A less restrictive but possible answer for OP could be:

\bkey="(abcd(?:-\w+)*-xyz(?:-\w+)*)"

See the online demo

JvdV
  • 70,606
  • 8
  • 39
  • 70
  • Have updated the question by explaining the full input on which the regex has to be applied. sorry for missing. can u pls update the answer? For single key, value its working. But the key value is part of the large string in the question i edited. Thanks. – CdVr Jul 15 '20 at 07:13
  • @JvdV how did you generate that image? – Bren Jul 15 '20 at 07:27
  • @Bren. I got it from [here](https://www.debuggex.com/) – JvdV Jul 15 '20 at 07:33
  • @JvdV that is fine in the editor. But my real implementation lies in another app which is not returning anything. but it returns the Group 1 value which is in right pane of the [link](https://regex101.com/r/QaFWIu/3). Is there anyway to update the existing one in the link with using the group concept? – CdVr Jul 15 '20 at 07:34
  • What is this "app" you are talking about. Looks like you might want to update your OP with some more relevant information. If you can't work with groups, try: `(?<=\bkey=")abcd(?=.*-xyz\b)(?:-[a-z]+){4}(?=")`. It's basically the same validation but with positive lookarounds. – JvdV Jul 15 '20 at 07:39
  • Its a dashboard. you can refer this link and search for `Regex: /"([^"]+)"/` in the [link](https://grafana.com/docs/grafana/latest/features/datasources/prometheus/#query-variable) This regex is applied on the query result which is a string i referred. Its working with that regex. I'm trying to alter with that regexGroup itself. Would this help? @JvdV – CdVr Jul 15 '20 at 07:45
  • You should update your original question with this information for others to read too. I also have no idea what it is you are currently trying. You mentioned it is working now? – JvdV Jul 15 '20 at 07:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/217883/discussion-between-venkat-and-jvdv). – CdVr Jul 15 '20 at 07:59
0

Will this work?

Pattern

\bkey="(abcd-[^"]*\bxyz\b[^"]*)"

Demo

Bren
  • 605
  • 4
  • 15
  • Have updated the question by explaining the full input on which the regex has to be applied. sorry for missing. can u pls update the answer? For single key, value its working. But the key value is part of the large string in the question i edited. Thanks. – CdVr Jul 15 '20 at 07:13
  • @venkat try now – Bren Jul 15 '20 at 07:23
0

You could use the following regular expression to verify the string has the desired format and to match the portion of the string that is of interest.

(?<=\bkey=")(?=.*-xyz(?=-|$))abcd(?:-[a-z]+)+(?=")

Start your engine!

Note there are no capture groups.

The regex engine performs the following operations.

(?<=\bkey=")  : positive lookbehind asserts the current
                position in the string is preceded by 'key=' 
(?=           : begin positive lookahead
  .*-xyz      : match 0+ characters, then '-xyz'
  (?=-|$)     : positive lookahead asserts the current position is
              : followed by '-' or is at the end of the string
)             : end non-capture group
abcd          : match 'abcd'
(?:           : begin non-capture group
  -[a-z]+     : match '-' followed by 1+ characters in the class
)+            : end non-capture group and execute it 1+ times
(?=")         : positive lookahead asserts the current position is
              : followed by '"'
Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
  • I've just found out `Prometheus` used `RE2` engine =( – JvdV Jul 15 '20 at 08:11
  • @JvdV, I don't think so. Aside from the fact that Prometheus wasn't even a real person, the RE2 engine was released well after the time of his exploits. – Cary Swoveland Jul 15 '20 at 08:29
  • I see what you did there =). Note to self: watch my English around Cary. – JvdV Jul 15 '20 at 08:30
  • I don't find a way to negate a pattern. Is there any workaround for that? It is similar to following SOF question [reference link](https://stackoverflow.com/questions/54225052/negative-lookahead-work-around-for-re2-syntax) – CdVr Jul 22 '20 at 15:48