I have a question on parsing multiple repeating group using regex. A user might enter some query, which i need to parse and construct the correct sql statement out of it. Someone might enter:
Artist:Rolling Stones
Artist:"Rolling Stones"
Artist:"Rolling Stones" AND Title:Satisfaction
Artist:"Rolling Stones" AND Title:Satisfaction AND FileType:mp3
This regex would parse the first 2 samples:
(([A-Za-z]. ?):("?. "?)\s(AND|and|Or|or)?)
But i could have 1,2 or n of the same group. Is there something, where i could say repeat that multiple times and use the same pattern to match also the Title in sample 3 and Title and FileType in sample 4.
I came up with this:
(([A-Za-z]. ?):("?. "?)\s(AND|and|Or|or)?)\s ?(([A-Za-z]. ?):("?. "?)(AND|and|Or|or)?)?
That would capture sample 3, but does not match on 1 and 2.
Any help is appreciated. thanks
CodePudding user response:
I don't know if regex is the best tool for the job, but I came up with
(\w ?\:\"?. ?\"?(?=(\sAND|\sOR|$)))
You can see it matches your tokens, Explanation: it uses positive lookahead to ensure every token has after AND OR or a line termination, so your 1st and 3rd match
In your fist group you will find your query and in the second group you will find the ANDs and ORs
Explanation: uses lookahead to find the words AND or OR to delimit the tokens, also considering end of line to match single tokens