/** * * @author Zen Johnny * @date April 29, 2018 PM 4:31:07 * */package Demo.regex;public class Regexreplacedemo {public S tatic void Replacedemo (String string, string regex,string replacement) {System.out.println (String.replaceall (Regex, replacement));} public static void Main (string args[]) {//case1: As long as the number is more than 5 consecutive, the segment string is replaced with #string string = " Bn45353453assss3444effffewtvdt4efvdfdgf455454ggt5grgfer3 "; String regex = "\\d{5,}+"; string replacement = "#"; Replacedemo (string, regex, replacement);//bn#assss3444effffewtvdt4efvdfdgf#ggt5grgfer3// CASE2: Replace the overlapping string with a single character//string = ""; regex = "(.) \\1+ "; replacement =" $ ";//" Focus "$: Take the first group in the previous rule Replacedemo (string, regex, replacement);// Bn45353453as34efewtvdt4efvdfdgf45454gt5grgfer3}}
Java se Regular expression three: replace