首页 > 学院 > 开发设计 > 正文

常用类-正则表达式、Random、System

2019-11-07 23:00:24
字体:
来源:转载
供稿:网友

1:正则表达式(理解) (1)就是符合一定规则的字符串 (2)常见规则 A:字符 x 字符 x。举例:’a’表示字符a / 反斜线字符。 /n 新行(换行)符 (‘/u000A’) /r 回车符 (‘/u000D’)

B:字符类 [abc] a、b 或 c(简单类) [^abc] 任何字符,除了 a、b 或 c(否定) [a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围) [0-9] 0到9的字符都包括 C:预定义字符类 . 任何字符。我的就是.字符本身,怎么表示呢? /. /d 数字:[0-9] /w 单词字符:[a-zA-Z_0-9] 在正则表达式里面组成单词的东西必须有这些东西组成 D:边界匹配器 ^ 行的开头 $ 行的结尾 /b 单词边界 就是不是单词字符的地方。 举例:hello world?haha;xixi E:Greedy 数量词 X? X,一次或一次也没有 X* X,零次或多次 X+ X,一次或多次 X{n} X,恰好 n 次 X{n,} X,至少 n 次 X{n,m} X,至少 n 次,但是不超过 m 次 (3)常见功能:(分别用的是谁呢?) A:判断功能 String类的public boolean matches(String regex)/* * 判断功能 * String类的public boolean matches(String regex) * * 需求: * 判断手机号码是否满足要求? * * 分析: * A:键盘录入手机号码 * B:定义手机号码的规则 * 13436975980 * 13688886868 * 13866668888 * 13456789012 * 13123456789 * 18912345678 * 18886867878 * 18638833883 * C:调用功能,判断即可 * D:输出结果 */public class RegexDemo { public static void main(String[] args) { //键盘录入手机号码 Scanner sc = new Scanner(System.in); System.out.PRintln("请输入你的手机号码:"); String phone = sc.nextLine(); //定义手机号码的规则 String regex = "1[38]//d{9}"; //调用功能,判断即可 boolean flag = phone.matches(regex); //输出结果 System.out.println("flag:"+flag); }} B:分割功能 String类的public String[] split(String regex)/* * 分割功能 * String类的public String[] split(String regex) * 根据给定正则表达式的匹配拆分此字符串。 * * 举例: * 百合网,世纪佳缘,珍爱网,QQ * 搜索好友 * 性别:女 * 范围:"18-24" * * age>=18 && age<=24 */public class RegexDemo { public static void main(String[] args) { //定义一个年龄搜索范围 String ages = "18-24"; //定义规则 String regex = "-"; //调用方法 String[] strArray = ages.split(regex);// //遍历// for(int x=0; x<strArray.length; x++){// System.out.println(strArray[x]);// } //如何得到int类型的呢? int startAge = Integer.parseInt(strArray[0]); int endAge = Integer.parseInt(strArray[1]); //键盘录入年龄 Scanner sc = new Scanner(System.in); System.out.println("请输入你的年龄:"); int age = sc.nextInt(); if(age>=startAge && age<=endAge) { System.out.println("你就是我想找的"); }else { System.out.println("不符合我的要求,gun"); } }} C:替换功能 String类的public String replaceAll(String regex,String replacement)/* * 替换功能 * String类的public String replaceAll(String regex,String replacement) * 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。 */public class RegexDemo { public static void main(String[] args) { // 定义一个字符串 String s = "helloqq12345worldkh622112345678java"; // 我要去除所有的数字,用*给替换掉 // String regex = "//d+"; // String regex = "//d"; //String ss = "*"; // 直接把数字干掉 String regex = "//d+"; String ss = ""; String result = s.replaceAll(regex, ss); System.out.println(result); }} D:获取功能 Pattern和Matcher Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("aaaaab"); find():查找存不存在 group():获取刚才查找过的数据/* * 获取功能 * Pattern和Matcher类的使用 * * 模式和匹配器的基本使用顺序 */public class RegexDemo { public static void main(String[] args) { // 模式和匹配器的典型调用顺序 // 把正则表达式编译成模式对象 Pattern p = Pattern.compile("a*b"); // 通过模式对象得到匹配器对象,这个时候需要的是被匹配的字符串 Matcher m = p.matcher("aaaaab"); // 调用匹配器对象的功能 boolean b = m.matches(); System.out.println(b); //这个是判断功能,但是如果做判断,这样做就有点麻烦了,我们直接用字符串的方法做 String s = "aaaaab"; String regex = "a*b"; boolean bb = s.matches(regex); System.out.println(bb); }}(4)案例 A:判断电话号码和邮箱/* * 校验邮箱 * * 分析: * A:键盘录入邮箱 * B:定义邮箱的规则 * 1517806580@qq.com * liuyi@163.com * linqingxia@126.com * fengqingyang@sina.com.cn * fqy@itcast.cn * C:调用功能,判断即可 * D:输出结果 */public class RegexTest { public static void main(String[] args) { //键盘录入邮箱 Scanner sc = new Scanner(System.in); System.out.println("请输入邮箱:"); String email = sc.nextLine(); //定义邮箱的规则 //String regex = "[a-zA-Z_0-9]+@[a-zA-Z_0-9]{2,6}(//.[a-zA-Z_0-9]{2,3})+"; String regex = "//w+@//w{2,6}(//.//w{2,3})+"; //调用功能,判断即可 boolean flag = email.matches(regex); //输出结果 System.out.println("flag:"+flag); }} B:按照不同的规则分割数据/* * 我有如下一个字符串:"91 27 46 38 50" * 请写代码实现最终输出结果是:"27 38 46 50 91" * * 分析: * A:定义一个字符串 * B:把字符串进行分割,得到一个字符串数组 * C:把字符串数组变换成int数组 * D:对int数组排序 * E:把排序后的int数组在组装成一个字符串 * F:输出字符串 */public class RegexTest { public static void main(String[] args) { // 定义一个字符串 String s = "91 27 46 38 50"; // 把字符串进行分割,得到一个字符串数组 String[] strArray = s.split(" "); // 把字符串数组变换成int数组 int[] arr = new int[strArray.length]; for (int x = 0; x < arr.length; x++) { arr[x] = Integer.parseInt(strArray[x]); } // 对int数组排序 Arrays.sort(arr); // 把排序后的int数组在组装成一个字符串 StringBuilder sb = new StringBuilder(); for (int x = 0; x < arr.length; x++) { sb.append(arr[x]).append(" "); } //转化为字符串 String result = sb.toString().trim(); //输出字符串 System.out.println("result:"+result); }} C:把论坛中的数字替换为* D:获取字符串中由3个字符组成的单词/* * 获取功能: * 获取下面这个字符串中由三个字符组成的单词 * da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu? */public class RegexDemo2 { public static void main(String[] args) { // 定义字符串 String s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?"; // 规则 String regex = "//b//w{3}//b"; // 把规则编译成模式对象 Pattern p = Pattern.compile(regex); // 通过模式对象得到匹配器对象 Matcher m = p.matcher(s); // 调用匹配器对象的功能 // 通过find方法就是查找有没有满足条件的子串 // public boolean find() // boolean flag = m.find(); // System.out.println(flag); // // 如何得到值呢? // // public String group() // String ss = m.group(); // System.out.println(ss); // // // 再来一次 // flag = m.find(); // System.out.println(flag); // ss = m.group(); // System.out.println(ss); while (m.find()) { System.out.println(m.group()); } // 注意:一定要先find(),然后才能group() // IllegalStateException: No match found // String ss = m.group(); // System.out.println(ss); }}

2:Random(理解) (1)用于产生随机数的类 (2)构造方法: A:Random() 默认种子,每次产生的随机数不同 B:Random(long seed) 指定种子,每次种子相同,随机数就相同 (3)成员方法: A:int nextInt() 返回int范围内的随机数 B:int nextInt(int n) 返回[0,n)范围内的随机数

/* * Random:产生随机数的类 * * 构造方法: * public Random():没有给种子,用的是默认种子,是当前时间的毫秒值 * public Random(long seed):给出指定的种子 * * 给定种子后,每次得到的随机数是相同的。 * * 成员方法: * public int nextInt():返回的是int范围内的随机数 * public int nextInt(int n):返回的是[0,n)范围的内随机数 */public class RandomDemo { public static void main(String[] args) { // 创建对象 // Random r = new Random(); Random r = new Random(1111); for (int x = 0; x < 10; x++) { // int num = r.nextInt(); int num = r.nextInt(100) + 1; System.out.println(num); } }}

3:System(掌握) (1)系统类,提供了一些有用的字段和方法

/* * System类包含一些有用的类字段和方法。它不能被实例化。 * * 方法: * public static void gc():运行垃圾回收器。 * public static void exit(int status) * public static long currentTimeMillis() * public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length) */public class SystemDemo { public static void main(String[] args) { Person p = new Person("赵雅芝", 60); System.out.println(p); p = null; // 让p不再指定堆内存 System.gc(); }}(2)成员方法(自己补齐) A:运行垃圾回收器 B:退出jvm C:获取当前时间的毫秒值 D:数组复制/* * System类包含一些有用的类字段和方法。它不能被实例化。 * * 方法: * public static void gc():运行垃圾回收器。 * public static void exit(int status):终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。 * public static long currentTimeMillis():返回以毫秒为单位的当前时间 * public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length) */public class SystemDemo { public static void main(String[] args) { // System.out.println("我们喜欢林青霞(东方不败)"); // System.exit(0); // System.out.println("我们也喜欢赵雅芝(白娘子)"); // System.out.println(System.currentTimeMillis()); // 单独得到这样的实际目前对我们来说意义不大 // 那么,它到底有什么作用呢? // 要求:请大家给我统计这段程序的运行时间 long start = System.currentTimeMillis(); for (int x = 0; x < 100000; x++) { System.out.println("hello" + x); } long end = System.currentTimeMillis(); System.out.println("共耗时:" + (end - start) + "毫秒"); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表