I am attempting to write a regular expression to process a string of key value(s) pairs formatted like so
KEY/VALUE KEY/VALUE VALUE KEY/VALUE
A key can have multiple values separated by a space.
I want to match a keys values together, so the result on the above string would be
VALUE
VALUE VALUE
VALUE
I currently have the following as my regex
[A-Z0-9]+/([A-Z0-9 ]+)(?:(?!^[A-Z0-9]+/))
but this returns
VALUE KEY
as the first result.
In your negative lookahead assertion, change
+to*; otherwise, you’re not preventing the match from ending right before a/, you’re only preventing it from ending right before a word that’s followed by a/. Also, remove the^from your negative lookahead assertion; it means “beginning of string”, so will never match in this context. That leaves:(I also dropped the
(?:...)notation, since it had no effect in the context where it appeared.)That said, a somewhat easier-to-read approach might be this:
which requires the value to be followed by either a space (which gets swallowed) or end-of-string. Since keys are followed by
/, it will ignore them.