My regex skills are not very good and recently a new data element has thrown my parser into a loop
Take the following string
“+USER=Bob Smith-GROUP=Admin+FUNCTION=Read/FUNCTION=Write”
Previously I had the following for my regex : [+\\-/]
Which would turn the result into
USER=Bob Smith
GROUP=Admin
FUNCTION=Read
FUNCTION=Write
FUNCTION=Read
But now I have values with dashes in them which is causing bad output
New string looks like “+USER=Bob Smith-GROUP=Admin+FUNCTION=Read/FUNCTION=Write/FUNCTION=Read-Write”
Which gives me the following result , and breaks the key = value structure.
USER=Bob Smith
GROUP=Admin
FUNCTION=Read
FUNCTION=Write
FUNCTION=Read
Write
Can someone help me formulate a valid regex for handling this or point me to some key / value examples. Basically I need to be able to handle + – / signs in order to get combinations.
You didn’t specify what regex engine you’re using, but this works if you’ve got lookahead/lookbehind.
It works on the premise that the keys are all uppercase only, whilst the values aren’t – not sure if that’s a valid assumption, but if it’s not then as noted things will get complicated and messy.
And here’s my attempt to explain that (not sure how much this makes sense):
For Java string->regex, backslashes need escaping (as would quotes, if there were any):
And if capturing groups are needed, just add parens round the appropriate parts:
The matching part of this, to turn it into newline delimited text, is something like…