3

I have this regex:

(?:(?:AND\sNOT|AND|OR)(?!.*(?:AND\sNOT|AND|OR))\s)(.*)

What I want is to get the last key:value pair, example -

k:v AND k1:v1 AND NOT k2:v2 OR k3:v3

I want the regex to match k3:v3, and it does, but it doesn't match the key:value in the next situation -

k:v

I need it to match the key:value pair even if it's the first one and there's no operators brefore...

update: key:value is not the issue here, I need it to match everything after the last operator

update 2: tried to do the following -

(?:(?:AND\sNOT|AND|OR)?(?!.*(?:AND\sNOT|AND|OR))\s)(.*)
(?:(?:AND\sNOT|AND|OR?)(?!.*(?:AND\sNOT|AND|OR))\s)(.*)

didn't work

3
  • Maybe (?:.*(?:AND\sNOT|AND|OR)\s)?(.*)$ Commented Feb 25, 2018 at 13:01
  • Can you post your comment as an answer so I could vote it? Commented Feb 25, 2018 at 14:11
  • Added and updated regex! Commented Feb 25, 2018 at 14:40

3 Answers 3

2

Edit - My bad, \K isn't supported in JS. Here's an alternative one:

/(?:.*(?:AND|OR|NOT)\s+)?(.*)/ (group 1)

https://regex101.com/r/AyVV89/3 (Please check the matches on the right panel, for some reason group 1 isn't highlighted on the text.)

Original post

Per your last update (that wasn't obvious at all before you mentioned it):

key:value is not the issue here, I need it to match everything after the last operator

This one will match anything after the last operator, no matter what it is (and work without an operator too):

/(?:.*(?:AND|OR|NOT)\s+\K)?.*/

https://regex101.com/r/AyVV89/2

Sign up to request clarification or add additional context in comments.

2 Comments

Javascript doesn't support \K.
You're right, didn't realize that. Will edit/delete my answer then, thanks.
1

Update

As per comments, I assume you need to match last key:value pair along with its operator if any, and anything that comes after:

(?:.*\b(AND(?:\sNOT)?|OR) +)?(\S+:\S+.*)

Live demo


What you need could be simplified into this:

(?:AND(?:\sNOT)?|OR)\s+(\S+)$

Live demo

  • (?:AND(?:\sNOT)?|OR) match AND or AND NOT or OR
  • \s+ match any number of whitespaces
  • (\S+) match and capture none-whitespace characters
  • $ asserts end of string

6 Comments

Your regex is not working as expected: regex101.com/r/JXXjWk/2
Well are you seeking for \S+$? Look at this demo.
No, your regex is not including white spaces event if it's not operator between them.. look at my question again.. anyway S.Jovan posted the correct regex
regex101.com/r/yniN74/2 it matches the last word, not everything after the operator... I need the operators to be the separator, not white spaces
So you need everything after last key:value pair? here asd asd?
|
0

From your explanation, you don't need the first operator at all. Simply try

(?!.*(?:NOT|AND|OR)\s)(\b.*)

1 Comment

your regex is not valid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.