import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
boolean flag = true;
Scanner sc = new Scanner(System.in);
System.out.println("请输入qq号码");
String qq = sc.nextLine();
if(qq.length()>=5 && qq.length()<=12){
char c = qq.charAt(0);
if(c!='0'){
for(int i=0;i<qq.length();i++){
char ch = qq.charAt(i);
if(!Character.isDigit(ch)){
flag = false;
}
}
}else{
flag = false;
}
}else{
flag = false;
}
if(flag){
System.out.println("qq格式正确");
}else{
System.out.println("qq格式不正确");
}
}
}
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入qq号码");
String qq = sc.nextLine();
String regex = "[1-9][0-9]{4,14}";
boolean flag = qq.matches(regex);
if(flag){
System.out.println("qq格式正确");
}else{
System.out.println("qq格式不正确");
}
}
}
/*
* 正则表达式的规则组成
* A:字符
* x 字符 x。举例:'a'表示字符a
* \\ 反斜线字符。
* \n 新行(换行)符 ('\u000A')
* \r 回车符 ('\u000D')
* B:字符类
* [abc] a、b 或 c(简单类)
* [^abc] 任何字符,除了 a、b 或 c(否定)
* [0-9] 0到9的字符都包括
* [a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
* [a-zA-Z_0-9] a到 z 或 A到 Z 或0-9,两头的字母包括在内(范围)
* C:预定义字符类
* . 任何字符。我的就是.字符本身,怎么表示呢? \.
* \d 数字:[0-9]
* \w 单词字符:[a-zA-Z_0-9]
* D:边界匹配器
* ^ 行的开头
* $ 行的结尾
* \b 单词边界
就是不是单词字符的地方。
举例:hello world?haha;xixi
* E:Greedy 数量词
* X? X,一次或一次也没有 0-1 次
* X* X,零次或多次 0-n;
* X+ X,一次或多次 1-n
* X{n} X,恰好 n 次
* X{n,} X,至少 n 次
* X{n,m} X,至少 n 次,但是不超过 m 次
*
*/
public class Demo {
public static void main(String[] args) {
String s = "1234567";
String regex = "[0-9]{3,6}";
System.out.println(s.matches(regex));
}
}
Demo
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入电话号码");
String phone = sc.nextLine();
String regex = "1[3-9][0-9]{9}";
boolean flag = phone.matches(regex);
if(flag){
System.out.println("电话格式正确");
}else{
System.out.println("电话格式不正确");
}
}
}
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入email号码");
String email = sc.nextLine();
String regex = "\\w{6,15}@\\w{2,5}(\\.\\w{2,5})+";
boolean flag = email.matches(regex);
if(flag){
System.out.println("email格式正确");
}else{
System.out.println("email格式不正确");
}
}
}
正则表达式的分割功能
public class Demo {
public static void main(String[] args) {
String str = "D:\\java1129\\第二十三天-XML解析\\视频";
String regex = "\\\\";
String[] split = str.split(regex);
for (String s : split) {
System.out.println(s);
}
}
}
import java.util.Arrays;
public class Demo2 {
public static void main(String[] args) {
String str = "91 27 46 38 50";
String regex = " +";
String[] split = str.split(regex);
int[] arr = new int[split.length];
for (int i = 0;i<split.length;i++) {
arr[i] = Integer.parseInt(split[i]);
}
Arrays.sort(arr);
String s1 = "";
StringBuffer sb = new StringBuffer();
for (int i : arr) {
sb.append(i).append(" ");
}
System.out.println("原始字符串:"+str);
System.out.println("分割排序字符串:"+sb.toString());
}
}
正则表达式的替换功能
public class Demo {
public static void main(String[] args) {
String str = "你好吗这是我的银行账号 12456789";
String regex = "[0-9]";
str = str.replaceAll(regex, "*");
System.out.println(str);
}
}
正则表达式的获取功能
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo {
public static void main(String[] args) {
String str = "da jia ting wo shuo, jin tian yao xia yu. bu shang wan zi xi, gao xing bu?";
String regex = "\\b[a-z]{3}\\b";
Pattern pt = Pattern.compile(regex);
Matcher matcher = pt.matcher(str);
while(matcher.find()){
System.out.println(matcher.group());
}
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo2 {
public static void main(String[] args) throws IOException {
String regex = "1[3-9][0-9]{9}";
Pattern p = Pattern.compile(regex);
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
String str = null;
while((str = br.readLine())!=null){
Matcher matcher = p.matcher(str);
while(matcher.find()){
System.out.println(matcher.group());
}
}
}
}