(转)学习正则表达式最佳的材料   Leave a comment

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3. public class RegularExpression {
  4.     public static void main(String[] args) {
  5.         // 简单认识正则表达式的概念
  6.         // p(“abc”.matches(“…”));
  7.         // p(“a8729a”.replaceAll(“\\d”, “-“));
  8.         // Pattern p = Pattern.compile(“[a-z]{3}”);
  9.         // Matcher m = p.matcher(“fgh”);
  10.         // p(m.matches());
  11.         // p(“aaas”.matches(“[a-z]{3,}”));
  12.         // 初步认识. * + ?
  13.         // p(“a”.matches(“.”));
  14.         // p(“aa”.matches(“aa”));
  15.         // p(“aaaa”.matches(“a*”));
  16.         // p(“aaaa”.matches(“a+”));
  17.         // p(“”.matches(“a*”));
  18.         // p(“aaaa”.matches(“a?”));
  19.         // p(“”.matches(“a?”));
  20.         // p(“a”.matches(“a?”));
  21.         // p(“214523145234532”.matches(“\\d{3,100}”));
  22.         // p(“192.168.0.231”.matches(“\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}”));
  23.         // p(“192”.matches(“[0-2][0-9][0-9]”));
  24.         // 范围
  25.         // p(“a”.matches(“[abc]”));
  26.         // p(“a”.matches(“[^abc]”));
  27.         // p(“A”.matches(“[a-zA-Z]”));
  28.         // p(“a”.matches(“[a-z]|[A-Z]”));
  29.         // p(“A”.matches(“[a-z[A-Z]]”));
  30.         // p(“R”.matches(“[A-Z&&[RFG]]”));
  31.         // 认识\s \w \d \
  32.         // p(” \n\r\t”.matches(“\\s{4}”));
  33.         // p(” “.matches(“\\S”));
  34.         // p(“a_8”.matches(“\\w{3}”));
  35.         // p(“abc888&^%”.matches(“[a-z]{1,3}\\d+[&^#%]+”));
  36.         // p(“\\”.matches(“\\\\”));
  37.         // POSIX Style
  38.         // p(“a”.matches(“\\p{Lower}”));
  39.         // boundary
  40.         // p(“hello sir”.matches(“^h.*”));
  41.         // p(“hello sir”.matches(“.*ir$”));
  42.         // p(“hello sir”.matches(“^h[a-z]{1,3}o\\b.*”));
  43.         // p(“hellosir”.matches(“^h[a-z]{1,3}o\\b.*”)); // whilte lines
  44.         // p(“\n”.matches(“^[\\s&&[^\\n
  45.         // p(“aaa 8888c”.matches(“.*\\d{4}.”));
  46.         // p(“aaa8888c”.matches(“.*\\b\\d{4}.”));
  47.         // p(“aaa8888c”.matches(“.*\\d{4}.”));
  48.         // p(“aaa 8888c”.matches(“.*\\b\\d{4}.”));
  49.         // email
  50.         // p(“asdfasdfsafsf@dsdfsdf.com”.matches(“[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+”));
  51.         // matches find lookingAt
  52.         // Pattern p = Pattern.compile(“\\d{3,5}”);
  53.         // String s = “123-34345-234-00”;
  54.         // Matcher m = p.matcher(s);
  55.         // p(m.matches());
  56.         // m.reset();
  57.         // p(m.find());
  58.         // p(m.start() + “-” + m.end());
  59.         // p(m.find());
  60.         // p(m.start() + “-” + m.end());
  61.         // p(m.find());
  62.         // p(m.start() + “-” + m.end());
  63.         // p(m.find());
  64.         // //p(m.start() + “-” + m.end());
  65.         // p(m.lookingAt());
  66.         // p(m.lookingAt());
  67.         // p(m.lookingAt());
  68.         // p(m.lookingAt());
  69.         // replacement
  70.         // Pattern p = Pattern.compile(“java”, Pattern.CASE_INSENSITIVE);
  71.         // Matcher m = p
  72.         // .matcher(“java Java JAVa JaVa IloveJAVA you hateJava afasdfasdf”);
  73.         // StringBuffer buf = new StringBuffer();
  74.         // int i = 0;
  75.         // while (m.find()) {
  76.         // i++;
  77.         // if (i % 2 == 0) {
  78.         // m.appendReplacement(buf, “java”);
  79.         // } else {
  80.         // m.appendReplacement(buf, “JAVA”);
  81.         // }
  82.         // }
  83.         // m.appendTail(buf);
  84.         // p(buf);
  85.         // group
  86.         // Pattern p = Pattern.compile(“(\\d{3,5})([a-z]{2})”);
  87.         // String s = “123aa-34345bb-234cc-00”;
  88.         // Matcher m = p.matcher(s);
  89.         // while (m.find()) {
  90.         // p(m.group());
  91.         // }
  92.         // qulifiers
  93.         // Pattern p = Pattern.compile(“.{3,10}+[0-9]”);
  94.         // String s = “aaaa5bbbb68”;
  95.         // Matcher m = p.matcher(s);
  96.         // if (m.find())
  97.         // p(m.start() + “-” + m.end());
  98.         // else
  99.         // p(“not match!”);
  100.         // non-capturing groups
  101.         // Pattern p = Pattern.compile(“.{3}(?=a)”);
  102.         // String s = “444a66b”;
  103.         // Matcher m = p.matcher(s);
  104.         // while (m.find()) {
  105.         // p(m.group());
  106.         // }
  107.         // back refenrences
  108.         // Pattern p = Pattern.compile(“(\\d(\\d))\\2”);
  109.         // String s = “122”;
  110.         // Matcher m = p.matcher(s);
  111.         // p(m.matches());
  112.         // flags的简写
  113.         Pattern p = Pattern.compile(“java”, Pattern.CASE_INSENSITIVE);
  114.         p(“Java”.matches(“(?i)(java)”));
  115.     }
  116.     public static void p(Object o) {
  117.         System.out.println(o);
  118.     }
  119. }

Posted 2009年03月16日 by gw8310 in java

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: