Java: Talking about regular expressions

1. Regular expressions

The English name of a regular expression is Regular Expression, which is a string consisting of ordinary characters (such as characters a~z) and special characters (metacharacters). It uses a single string to describe and define matching rules. A series of strings conforming to a certain grammatical rule. In actual development, it is often used to retrieve and replace text that meets a certain rule.

2. Regular expression matching rules

The grammatical rules of regular expressions:

(1) Character

字符:x
含义:代表的是字符x
例子:匹配规则为“a”,匹配内容为"a"

字符:\\
含义:反斜线字符'\'
例子:匹配字符为"\\|,匹配字符内容为"\"

字符:\t
含义:制表符
例子:匹配规则为"\t",相当于Tab键,产生一个制表符空间

字符:\n
含义:换行符
例子:匹配规则为"\n",效果为就是换行,光标在原位置的下一行

字符:\t
含义:回车符
例子:匹配规则为"\t",效果为回车效果,光标来到下一行

(2) Characters

字符类:[abc]
含义:代表字符a、b或c
例子:匹配规则为"[abc]",匹配的内容为字符a,或者字符b,或者字符c

字符类:[^abc]
含义:代表除了字符a、b或c意外的任意字符
例子:匹配规则为"[^abc]",匹配的内容为除了字符a,或者字符b,或者字符c的任意一个字符

字符类:[a-zA-Z]
含义:代表a到z或A-Z,两头的字母包含在内
例子:匹配规则为"[a-zA-Z]",匹配一个大写或小写字母

字符类:[0-9]
含义:代表数字0到9,两头的数字包含在内
例子:匹配规则为"[0-9]",匹配的内容一个数字
字符类:[a-zA-Z_0-9]
含义:代表字符a、b或c
例子:匹配规则为"[a-zA-Z_0-9]",匹配的内容为一个数字或者一个字母或者一个下划线

(3) Predefined character class

预定义字符类:.
含义:代表的任何字符
例子:匹配规则为:".",匹配的是一个任意字符。使用.需要使用匹配规则"\\."来实现

预定义字符类:\d
含义:代表09的数,两端包含在内,相当于[0-9]
例子:匹配规则为:"\d",匹配的是一个数字

预定义字符类:\w
含义:代表字母或者数字或者下划线(即单字符),相当于[a-zA-Z_0-9]
例子:匹配规则为:"\w",匹配的内容为一个数字或者一个字母或者一个下划线

(4) Boundary matcher

边界匹配器:^
含义:代表行的开头
例子:匹配规则为^[abc][0-9]$,匹配的内容从[abc]这个位置开始,相当于左双引号

边界匹配器:$
含义:代表行的结尾
例子:匹配规则为^[abc][0-9]$,匹配的内容以[0-9]结尾,相当于右双引号

边界匹配器:\b
含义:代表单词的边界
例子:匹配规则为\b[abc]\b,代表字母a或b或c的左右两边需要的是非单词字符([a-zA-Z_0-9])

边界匹配器:X?
含义:代表X出现一次或者一次也没有
例子:匹配规则为"a?",匹配的内容是一个字符a,或者一个a也没有

边界匹配器:X*
含义:代表行的开头
例子:匹配规则为"X*",匹配的内容是多个字符a,或者一个a也没有

(5) Quantifier

数量词:X+
含义:X出现一次或者多次
例子:匹配规则为"a+",匹配的内容是多个字符a,或者一个a

量词:X{
    
    n}
含义:X恰好出现n次
例子:匹配规则为"a{5}",匹配的内容是5个字符a

量词:X{
    
    n,}
含义:X至少出现n次
例子:匹配规则为"a{5,}",匹配的内容是最少有5个字符a

量词:X{
    
    n,m}
含义:X至少出现n次,但不超过m次
例子:匹配规则为"a{5,8}",匹配的内容是最少有5个字符a,但是不超过8

3. Three methods related to regular expressions in string types

(1)matches(String regex)

The function of this function is to tell that the string is a regular expression matched to a specific string.
Example: Check whether the QQ number is correct

package demo;

import java.util.Scanner;

public class RegexDemo {
    
    
	public static void main(String[] args) {
    
    
		String regex = "[1-9][0-9]{4,14}";
		System.out.println("请输入QQ号");
		Scanner input = new Scanner(System.in);
		String qq = input.next();
		boolean flag = qq.matches(regex);
		System.out.println(flag);
	}
}

String regex = "[1-9][0-9]{4,14}"; This sentence means that the QQ number starts with 1-9, the rest are any digits that can be 0-9, the QQ number is the most The length is 14 digits, and the shortest is 4 digits.

(2)split(String regex)

The function of this function is to split this string according to the regular expression match.
Example:

package demo;

import java.util.Scanner;

public class RegexDemo {
    
    
	public static void main(String[] args) {
    
    
		String regex = "[,。]";
		System.out.println("请输入要分割的字符串:");
		Scanner input = new Scanner(System.in);
		String s = input.nextLine();
		
		String ss[] = s.split(regex);
		for(String s1:ss) {
    
    
			System.out.println(s1);
		}
		
	}
}

Operation result: From this we can see that the string we input is separated by ",." as the boundary. Since the string becomes an array after separation, we need to use an array to receive it
Insert picture description here

(3)replaceAll(String regex,String replacement)

Use the specified replacement string to replace all matched regular expression substrings

package demo;

import java.util.Scanner;

public class RegexDemo {
    
    
	public static void main(String[] args) {
    
    
		Scanner input = new Scanner(System.in);
		System.out.println("请输入字符串:");
		String str = input.nextLine();
		str = str.replaceAll("[\\d+]", "#");
		System.out.println(str);
		
	}
}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43825377/article/details/109037509