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