Java学习第14天—一些常用类02

正则表达式:符合一定规则的字符串

字符

    x 字符   \\ 反斜线   \r 回车  \n 换行

字符类

    [abc] 任意一个     [a-z] 小写字符中的任意一个

预定义字符类

    . 任意字符,如果匹配.本身,\.      \d 数字字符,等价于[0-9]     \w 单词字符,等价于[a-zA-Z_0-9]

边界

    \b 单词边界,hello world?nihao+hehe

数量词

    X{n} 正好n次    X{n,} 至少n     X{n,m} nm

package com.nim.day14;

import java.util.Scanner;

/*
 * 判断手机号码	
 * 1、长度是11位
 * 2、以13/15/18开头
 * 3、纯数字
 */
public class RegexDemo2 {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		while(true){
			System.out.println("请输入手机号:");
			String num = s.next();
	        boolean bol = check(num);
	        System.out.println(bol);
		}
	}
		public static boolean check(String num){
			String pattern = "1[358][0-9]{9}";
			return num.matches(pattern);
		}
	}

	
package com.nim.day14;

import java.util.Scanner;

/*
 * 用正则表达式写一个方法根据电话区号判断归属地
 */

public class RegexDemo3 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		while(true){
			System.out.println("请输入电话号码:");
			String num = scanner.next();
			System.out.println(getProvince(num));
		}
		
	}
      //写一个方法判断电话号码归属地
	public static String getProvince(String n){
		String province = null;
		//定义正则表达式
		String bj = "010[0-9]{8}";
		String gd = "020[0-9]{8}";
		//剩下的就是其它的
		if(n.matches(bj)){
			return province="北京";
		}else if(n.matches(gd)){
			return province="广东";
		}else {
			return province = "others";
		}
		
	}
}

正则表达式两个特殊应用。1、两个"\\",要用"\\\\"来匹配;2、匹配点 ".",用"\\."。

package com.nim.day14;

public class RegexDemo4 {

	public static void main(String[] args) {
		/*
		//匹配路径之间的斜线\\
//		String path = "c:code\\demo\\day14";
		//也可以用左斜线进行分割路径,匹配的时候就用左斜线匹配
		String path = "c:code/demo/day14";
        String[] split = path.split("\\\\");
        for (int i = 0; i < split.length; i++) {
			System.out.println(split[i]);
			
		
		}
		*/
		//用“\\.”来匹配"."
		String s = "abc.bed.c";
		String[] split = s.split("\\.");
		for (int i = 0; i < split.length; i++) {
			System.out.println(split[i]);
		}
		
	}

}

模式和匹配器 ;模式和匹配器的应用: 获取字符串;replaceAll(“正则表达式(要替换的对象)”,“替换后的对象”);方法用正则表达式替换内容

package com.nim.day14;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexDemo5 {

	public static void main(String[] args) {
		/*
		// 模式与匹配器
		//编译正则表达式,产生pattern对象
		 Pattern p = Pattern.compile("a*b");
		 //调用pattern的matcher方法,产生一个匹配器
		 Matcher m = p.matcher("aaaaab");
		 //调用匹配器的matches方法,判断匹配器对象是否符合正则表达式
		 boolean b = m.matches();
		 System.out.println(b);
		
		 //模式和匹配器应用:获取字符串
	     String s = "a aa aaa bb bbb";
	     //写一个正则表达式的字符串
	     String pattern = "\\b\\w{2}\\b";
	     //将正则表达式的字符串编译成正则表达式
	     Pattern p = Pattern.compile(pattern);
	     //调用正则表达式的matcher方法,看字符串s是否匹配正则表达式
	     Matcher m = p.matcher(s);
	     
	     if(m.find()){
	    	 System.out.println(m.group());
	     }
	     
	     //用循环改写程序 m.find()方法 判断是否有匹配的 boolean类型返回值
	     while(m.find()){
	    	 //m.group返回在以前匹配操作期间由给定组捕获的输入子序列。
	    	 System.out.println(m.group());
	     }
	    
	     //找到四个字符中,第一个字符是T的字符
	     String ss = "Ther  class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class."
	     		+ " The Java Ab language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification."
	    		+ " Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown. "
	    		+ "A String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character class for more information). Index values refer to char code units, so a supplementary character uses two positions in a String. "
	    		+ "The String class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char values).";
	     String pattern = "\\b[aweAWTt][a-zA-z]{3}\\b";
	     Pattern p = Pattern.compile(pattern);
	     Matcher m = p.matcher(ss);
	     while(m.find()){
	    	 System.out.println(m.group());
	     }
	     */
		//replaceAll()
		String ss = "xiangcao:123456抹茶";
		//String res = ss.replaceAll("抹茶", "#");
		String res = ss.replaceAll("[0-9]", "*");
		System.out.println(res);
	}

}

猜你喜欢

转载自blog.csdn.net/chengming320/article/details/82426087
今日推荐