import java.util.*;
import java.util.regex.*;
import java.lang.*;
import java.io.*;

class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		String example = "            if    (ans(this) >= ans({1,2})  and (cond({3,4})  or ans(this) <= ans({5,6})), 7, 8)  and {111} > {222}  or ans(this) = \"hello    my friend and  or  \" and(cond({1,2}) $1 123    ";
		String rx = "\\s*\\b(and|or|if)\\b\\s*|(\"[^\"]*\")|(\\s+)";
		Matcher m = Pattern.compile(rx).matcher(example);
		example = m.replaceAll(r -> r.group(3) != null ? "" : r.group(2) != null ? r.group(2) : " " + r.group(1) + " ").trim();
		System.out.println( example );
		//E: if (ans(this)>=ans({1,2}) and (cond({3,4}) or ans(this)<=ans({5,6})),7,8) and {111}>{222} or ans(this)="hello    my friend and  or  " and(cond({1,2})$1123
		//C: 
	}
}