The regular expression of Java se three: replace

Source: Internet
Author: User
Tags java se

/** *  * @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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.