package org.fun.classdemo; import java.util.regex.Pattern; public class PatterinDemo { public static void main(String[] args) { Pattern pat = Pattern.compile("\\d+"); String str = "afde78555554774fg44"; String s[] = pat.split(str); for (int x = 0; x < s.length; x++) { System.out.println(s[x] + ","); } } }
package org.fun.classdemo; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherDemo01 { public static void main(String[] args) { String str = "111-22-333"; String pat = "\\d{3}-\\d{2}-\\d{3}"; Pattern p = Pattern.compile(pat); Matcher m = p.matcher(str); if (m.matches()) { System.out.println("success"); } else { System.out.println("failure"); } } }
package org.fun.classdemo; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherDemo02 { public static void main(String[] args) { String str = "df55gg33366gdff"; String pat = "\\d+"; Pattern p = Pattern.compile(pat); Matcher m = p.matcher(str); System.out.println(m.replaceAll("*")); } }