/** * Examples showing the usages of regular expression in String.replaceAll(). * @author Xuan Ngo */ public class Regex { public static void main(String[] args) { String s = ""; /* * Remove substring that ends with ,???$ * Show how to escape and use $. */ s = "53 001,88 $"; s = s.replaceAll(",...\\$$", "\\$"); System.out.println(s); // Output: 53 001$ /* * Replace a substring regardless of its characters' case(Case Insensitive). */ s = "CaSE Insensitive"; s = s.replaceAll("(?i)case", "Case"); System.out.println(s); // Output: Case Insensitive /* * Show how to use backreferences, reuse the matched pattern. */ s = "High Medium Low"; s = s.replaceAll("(High) (Medium) (Low)", "$3 $2 $1"); System.out.println(s); // Output: Low Medium High } }
// Separate words by capital letters
JAVA: s.replaceAll("((?<=[a-z])[A-Z]|[A-Z](?=[a-z]))", " $1");
PHP: preg_replace('((?<=[a-z])[A-Z]|[A-Z](?=[a-z]))', ' \\0', $string);