/* package whatever; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		test("hello's");
		test(";;;1235'5454!!");
		test(";;;1235'L454!!'");
	}
	
	static void test(String input) {
		String regex = regex(input);
		String replaceInBetween = replaceInBetween(input);
		System.out.println("regex: " + regex);
		System.out.println("rib:   " + replaceInBetween);
		System.out.println(regex.equals(replaceInBetween));
	}
	
	static String regex(String substr) {
		substr = substr.replaceAll("([^\\p{L}\\p{N}\\p{Mn}_\\-<>'])"," $1 ");
    	substr = substr.replaceAll("([\\p{N}\\p{L}])'(\\p{L})", "$1 '$2");
    	substr = substr.replaceAll("([^\\p{L}])'([^\\p{L}])", "$1 ' $2");
    	return substr;
	}
	
	static String replaceInBetween(String str) {
		StringBuilder sb = new StringBuilder();
		appendInBetween(sb, str, 0, str.length());
		return sb.toString();
	}
	
static void appendInBetween(StringBuilder resultStr, String s, int start, int end) {
  for (int i = start; i < end; ++i) {
    char c = s.charAt(i);

    // Check if c matches "([^\\p{L}\\p{N}\\p{Mn}_\\-<>'])".
    if (!(Character.isLetter(c)
          || Character.isDigit(c)
          || Character.getType(c) == Character.NON_SPACING_MARK
          || "_\\-<>'".indexOf(c) != -1)) {
      resultStr.append(' ');
      resultStr.append(c);
      resultStr.append(' ');
    } else if (c == '\'' && i > 0 && i + 1 < s.length()) {
      // We have a quote that's not at the beginning or end.

      char b = s.charAt(i - 1);
      char d = s.charAt(i + 1);

      if ((Character.isDigit(b) || Character.isLetter(b)) && Character.isLetter(d)) {
        // If the 3 chars match "([\\p{N}\\p{L}])'(\\p{L})"
        resultStr.append(' ');
        resultStr.append(c);
      } else if (!Character.isLetter(b) && !Character.isLetter(d)) {
        // If the 3 chars match "([^\\p{L}])'([^\\p{L}])"
        resultStr.append(' ');
        resultStr.append(c);
        resultStr.append(' ');
      } else {
        resultStr.append(c);
      }
    } else {
      // Everything else, just append.
      resultStr.append(c);
    }
  }
}
}