fork download
  1. import re
  2. text = r"""(@harrys OR from:harrys OR to:harrys OR ("harry's" OR harrys) AND (razor OR razors OR shave OR shaving OR shaved OR shaver OR subscription OR razorhead OR razorheads OR buy OR bought OR buying OR boxers OR cover) AND (has:geo OR has:profile_geo) -styles -prince -markle -meghanmarkle)"""
  3. pattern = r'("[^"]*"|\b(?:(?:OR|AND|sample)\b|(?:from|to|has):))|\w+'
  4. print( re.sub(pattern, lambda m: f'"{m.group(1)}"' if m.group(1) else m.group(), text) )
Success #stdin #stdout 0.04s 9344KB
stdin
Standard input is empty
stdout
(@harrys "OR" "from:"harrys "OR" "to:"harrys "OR" (""harry's"" "OR" harrys) "AND" (razor "OR" razors "OR" shave "OR" shaving "OR" shaved "OR" shaver "OR" subscription "OR" razorhead "OR" razorheads "OR" buy "OR" bought "OR" buying "OR" boxers "OR" cover) "AND" ("has:"geo "OR" "has:"profile_geo) -styles -prince -markle -meghanmarkle)