Reputation: 221
I have string like ((,"ab" ,,, , ,AND,,, ,, "cd" ,OR "ef" NOT, , "gh",))
. The words AND, OR,NOT can be either preceded, enclosed or followed by single of multiple commas. The comma itself can contain space between them.
I want the final string as ((,"ab" AND "cd" OR "ef" NOT "gh",))
. The regex I have tried is
(^|,+)AND|OR|NOT(?=,+|$)
But this is only able to capture ',AND'. How do I change my regex to even consider the commas which is followed by space and how do I capture the commas after the matched word?
Upvotes: 0
Views: 553
Reputation: 785276
You may try this re.sub
:
import re
s = '((,"ab" ,,, , ,AND,,, ,, "cd" ,OR "ef" NOT, , "gh",))'
r = re.sub(r'[\s,]+(AND|OR|NOT)[\s,]+', r' \1 ', s)
print (r)
Output:
((,"ab" AND "cd" OR "ef" NOT "gh",))
[,\s]+
matches 1+ of comma or whitespaces.(AND|OR|NOT)
= matches and captures these 3 words in group #1Upvotes: 3