A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Ethan丶 中级黑马   /  2015-9-19 22:52  /  773 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package com.itcast;

  2. /*
  3. * split(String regex)
  4. */
  5. public class RegexDemo3 {
  6.         public static void main(String[] args) {
  7.                 String s1 ="aa,bb,cc";
  8.                 String[] str = s1.split("//.");
  9.                 for(int x =0;x<str.length;x++){
  10.                         System.out.println(str[x]);
  11.                 }
  12.                 System.out.println("---");
  13.                 String s2="aa   bb      cc";
  14.                 String[] str2 = s2.split(" +");
  15.                 for(int x =0;x<str2.length;x++){
  16.                         System.out.println(str2[x]);
  17.                 }
  18.                 System.out.println("---");
  19.                 String s3="aa\\bb\\cc";
  20.                 String[] str3 = s3.split("\\\\");
  21.                 for(int x =0;x<str3.length;x++){
  22.                         System.out.println(str3[x]);
  23.                 }
  24.         }
  25. }
复制代码



A:字符
         x  字符x。
         \\ 反斜线字符。
         \n 换行
         \r 回车
         
B:字符类
          [abc]  或 a 或 b 或 c
          ['abc] 任何字符除了abc
          [a-zA-Z] 或a-z 或A-Z 包含AZ
          [0-9] 所有数字

C:预定义字符类
      . 任何字符   或 \.
      \d 数字:[0-9]
      \D 非数字: [^0-9]
      \w 单词字符:[a-zA-Z_0-9]
      \W 非单词字符:[^\w]
D:边界匹配器
        ^ 行的开头
        $ 行的结尾
        \b 单词边界
        \B 非单词边界
        \A 输入的开头
        \G 上一个匹配的结尾
        \Z 输入的结尾,仅用于最后的结束符(如果有的话)
        \z 输入的结尾

6 个回复

倒序浏览
正则表达式,神奇的东西
回复 使用道具 举报 1 0
  1. package com.itcast;
  2. /*
  3. * repalceAll(String regex,String replacement)
  4. * bbs.com abandon
  5. */
  6. public class RegexDemo4 {
  7.         public static void main(String[] args) {
  8.                 //randow
  9.                 String s ="hello3435sina201weibo23";
  10.                 // * replace
  11.                 String regex = "\\d";
  12.                 String ss = "*";
  13.                 System.out.println(s.replaceAll(regex, ss));
  14.         }
  15. }
复制代码



输出结果:hello****sina***weibo**
以前只能在别的地方看见  被和谐的,现在知道原理了
回复 使用道具 举报
Yingwenming 来自手机 中级黑马 2015-9-19 23:23:35
板凳
这贴太水了
回复 使用道具 举报
楼主输错了一个:转义字符
回复 使用道具 举报
清明月雨上 来自手机 中级黑马 2015-9-19 23:40:39
地板
能看懂即可 用的时候百度 老师原话
回复 使用道具 举报
Anmyre 中级黑马 2015-9-20 09:30:26
7#
正则用起来 挺好的,
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马