Regular expression of Java se V: Cutting

Source: Internet
Author: User
Tags java se

/** * * @author Zen Johnny * @date April 29, 2018 PM 3:53:55 * */package demo.regex;/* Regular expression: Cut */public class Regexsplitdemo {public static void Splitnames (String string) {string[] names = String.Split ("(, |\\s|\\.|;) + ");//cut by multiple spaces or commas or semicolons, do not use *for (String item:names) System.out.println (item);} Cut file directory public static void Splitdirs (String string) {string[] dirs = String.Split ("\\\\+"); for (string item:dirs) SYSTEM.OUT.PRINTLN (item);} public static void Namestest () {splitnames ("Zhang San, John Doe, Harry"); Splitnames ("Zhang San; John Doe,.; Harry ");/* Dick and Harry Harry Dick and Harry Harry */}public static void Dirstest () {splitdirs (" c:\\desktop\\project\\data.txt ");/*c:d Esktopprojectdata.txt*/}public static void Splitdemo (string string, string regex) {string[] items = string.split (regex); for (String item:items) System.out.println (item);} public static void Main (String args[]) {//namestest ();d irstest (); Splitdemo ("32432443styddxcccccdcfeweertryt", "(.) \\1+ ");//According to the overlapping words, the + number indicates that the preceding rules are executed multiple times/*" Focus: \1 "in order for the results of the rule to be reused, the rules can be encapsulated into a group and completed with (). The appearance of the group is numbered: starting from 1 You want to use an existing groupType: \ + group ordinal eg: with overlapping words as the separator symbol, "(.) \1 "*/}}

  

Regular expression of Java se V: Cutting

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.