Java tutorial
//package com.java2s; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static String processCaptionText(String text) { String result = text; Matcher matcher = Pattern.compile("#\\s*(\\w+)").matcher(text); ArrayList<Integer[]> toDelete = new ArrayList<>(); while (matcher.find()) { Integer[] tmpArray = new Integer[] { matcher.start(), matcher.end() }; toDelete.add(0, tmpArray); } for (Integer[] tmpArray : toDelete) { if (tmpArray[0] > 0 && result.substring(tmpArray[0] - 1, tmpArray[0]).compareTo(" ") == 0) tmpArray[0]--; if (tmpArray[0] == 0 && (tmpArray[1] + 1) < result.length()) tmpArray[1]++; result = result.substring(0, tmpArray[0]) + result.substring(tmpArray[1]); } return result; } }