ak1234
ak1234

Reputation: 221

Regex to find a particular word enclosed between commas python

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

Answers (1)

anubhava
anubhava

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",))

RegEx Demo

  • [,\s]+ matches 1+ of comma or whitespaces.
  • (AND|OR|NOT) = matches and captures these 3 words in group #1

Upvotes: 3

Related Questions