/** * * @author Zen Johnny * @date April 29, 2018 PM 4:51:08 * */package Demo.regex;import Java.util.regex.matcher;import J Ava.util.regex.pattern;public class Regexgetdemo {public static void Getdemo (string string, string regex) {//step1: Encapsulates a rule into an object, pattern pattern = pattern.compile (regex),//STEP2: Associate a regular object with the string to extract, get a match (engine) object Matcher Matcher = Pattern.matcher (string); while (Matcher.find ()) {//constantly using the regular engine to find objects that conform to regular expressions, found: true; not found: FalseSystem.out.println (" Start: "+ matcher.start () +" word: "+ matcher.group () +" End: "+ matcher.end ());//Returns the currently found substring and returns the beginning of its word start () and End Interval End ( )}}public static void Main (String args[]) {//To extract more than 3 words in a row//hint: \b: word boundary; \b Non-word boundary string string = "Ming Tian Jiu Yao Fang J IA la,da jia~ "; String regex = "\\b[a-za-z]{3,}\\b"; Getdemo (String,regex);/*mingtianjiuyaofangjiajia */}}
The regular expression of Java se four: get