Reputation: 19
I am trying to write condition in look behind to prevent space removing in quotes and after specific keywords.
For example, I have a string str = "client_id like 'TACIENT 1%' or client_id < 'TACLIENT'" ; Then I use regex: (?i)(?<!(eq|ne|<|>|like))\s(?!(eq|ne|<|>|like)) and output is: client_id like 'TACIENT1%' or client_id < 'TACLIENT'
But should be without space removing inside quotes: client_id like 'TACIENT 1%' or client_id < 'TACLIENT'
So regex to find text in single quotes is: '(?:[^']|\.)'* but when I put this regex in look behind: (?i)(?<!(eq|ne|<|>|like|'(?:[^']|\.)'))\s(?!(eq|ne|<|>|like))* I have an error that: "a quantifier inside a lookbehind makes it non fixed width" . When I put '?' after '*' quantifier it also doesn't helps So is there is any alternative way to handle it?
Upvotes: 1
Views: 1711
Reputation: 163577
Instead of using a lookbehind assertion with a quantifier, you can use a capture group to match from '...'
to "get that out of the way" and then use that capture group in the replacement.
The alternative will match a whitespace char with the assertions to the left and the right.
('(?:[^'\\]*(?:\\.[^'\\]*)*)')|(?<!eq|ne|[<>]|like)\s(?!eq|ne|[<>]|like)
Upvotes: 0