【转】java 正则表达式
上一篇 /
下一篇 2017-07-11 16:13:59
/ 个人分类:Java
- package pack.java.regexp;
-
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
-
-
-
-
-
- public class RegexpDemo01 {
-
- public static void main(String[] args) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- p("@1A中".matches("...."));
-
- p("abcd123abc".replaceAll("[0-9]{1}","-"));
-
- Pattern compile = Pattern.compile("\\w{3}");
- p(compile.matcher("aA1").matches());
-
- p("aA1bc".matches("\\w*"));
-
- p("AVB123a".matches("\\w+"));
-
- p("".matches("\\w?"));
-
- p("aaa".matches("a{3}"));
-
- p("123456".matches("\\d{3,}"));
-
- p("张AB/3&*".matches(".{3,100}"));
-
- p("192".matches("[0-2][0-9][0-9]"));
-
-
-
- p("a".matches("[abc]"));
-
- p("d".matches("[^abc]"));
-
- p("abc".matches("[a-zA-Z]{3}"));
-
- p("abez".matches("([a-z]|[A-Z]){4,}"));
-
- p("A".matches("[A-Z&&[ABC]]"));
-
-
- p(" \n\r\t".matches("\\s+"));
-
- p("a_z_2333aABD".matches("\\w{1,}"));
-
- p("abc3ABC$^&".matches("\\w{1,7}[%$&^]+"));
-
- p("\\".matches("\\\\"));
-
-
-
- p("hello zhangsan".matches("^h.*san$"));
-
- p("hello zhangabc".matches(".*abc$"));
-
- p(" \n".matches("^[\\s&&[^\\n]]*\\n$"));
-
-
-
- p("Yes sir.".matches("[a-z]*\\b.{1,}"));
- p("Yes\tsir.".matches("[a-z]*\\b.{1,}"));
-
-
-
- Pattern comp1 = Pattern.compile("\\d{3,5}");
- Matcher matcher = comp1.matcher("111-430-13331-234-13");
- p(matcher.matches());
-
- matcher.reset();
-
- p(matcher.find());
- p(matcher.start() + " - "+matcher.end());
- p(matcher.find());
- p(matcher.start() + " - "+matcher.end());
- p(matcher.find());
- p(matcher.start() + " - "+matcher.end());
- p(matcher.find());
- matcher.reset();
-
- p(matcher.lookingAt());
-
-
- Pattern comp2 = Pattern.compile("java",Pattern.CASE_INSENSITIVE);
- Matcher matcher2 = comp2.matcher("Java.Net JAVA JavA Java java JAVADemoJaVaDD I Love You JAva test abcd 123.");
- int i =0;
- StringBuffer sb = new StringBuffer();
- while(matcher2.find()){
- i++;
-
- if(i%2==0){
- matcher2.appendReplacement(sb, "JAVA");
- }else{
- matcher2.appendReplacement(sb, "java");
- }
- }
-
- matcher2.appendTail(sb);
- p(sb);
-
-
- Pattern comp3 = Pattern.compile("(\\d{3,5})([a-z]{1,3})", Pattern.CASE_INSENSITIVE);
- Matcher matcher3 = comp3.matcher("123a-234abc-33452SBD-32BS-311XYZ");
- while(matcher3.find()){
- p(matcher3.group(1));
- }
- }
-
-
-
-
-
- public static void p(Object obj){
- System.out.println(obj);
- }
- }
收藏
举报
TAG: