Common regular expressions and different writing methods in JS and Java

In the learning process, I believe you will often encounter regular expressions, and then check the Internet every time you need it, which is somewhat inconvenient, so here is a summary of some of your thoughts based on the premise of drawing on some other bloggers (thank you) More commonly used regular expressions. (It can be collected for convenience, hahaha, I don’t mind)

Verification date YYYY-MM-DD:

/^(\d{
    
    1,4})(-|\/)(\d{
    
    1,2})\2(\d{
    
    1,2})$/

Verify email:

/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/

Verification letter:

/^[A-Za-z]+$/

Verify integer:

/^[-+]?\d*$/

Verify non-negative integers:

/^\d+$ / 或 /^[1-9]\d*|0$/

Verification number:

/^-?\d*\.?\d+$/

Verify password strength (uppercase and lowercase English plus numbers and special symbols):

/^.*(?=.{
    
    6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/

Verify Chinese, English, numbers but not including symbols such as underscores:

/^[\u4E00-\u9FA5A-Za-z0-9]+$/ 或 /^[\u4E00-\u9FA5A-Za-z0-9]{
    
    2,20}$/

Verify English and numbers:

/^[A-Za-z0-9]+$/ 或 /^[A-Za-z0-9]{
    
    4,40}$/

Verify decimal:

/^[-\+]?\d+(\.\d+)?$/

Verify ID:

 /^\d{
    
    6}(18|19|20)?\d{
    
    2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{
    
    3}(\d|X)$/

Verify mobile phone number:

/^1[3456789]\d{
    
    9}$/

Verify Chinese:

/^[\u0391-\uFFE5]+$/

Verify Chinese, English, numbers including underscores:

/^[\u4E00-\u9FA5A-Za-z0-9_]+$/

Verify non-positive integers:

/^-[1-9]\d*|0$/ 或 /^((-\d+)|(0+))$/

Let's explain how to use regular in some different environments

Insert picture description here

Use cases of JS regular expressions:

Note: use diagonal bars to indicate the beginning and the end

	var str = document.getElementById("str").value.trim();//获取指定id的内容,去除空格
	var regex = /^[a-zA-Z]+$/; // 直接量正则表达式
	if(!regex.test(str)){
    
    
		alert("请输入正确的英文字母!");
	}

Use cases of Java regular expressions:

Note: Java does not need a diagonal start and end, and then "\" has to hit two! ! !

Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher("22bb23");
m.matches();//返回false,因为bb不能被\d+匹配,导致整个字符串匹配未成功.
Matcher m2=p.matcher("2223");
m2.matches();//返回true,因为\d+匹配到了整个字符串

Pattern.matcher(CharSequence input) returns a Matcher object

The construction method of the Matcher class is private and cannot be created at will. You can only get an instance of this class through the Pattern.matcher(CharSequence input) method.

The Pattern class can only do some simple matching operations. If you want to get stronger and more convenient regular matching operations, you need to cooperate Pattern and Matcher together.

The Matcher class provides grouping support for regular expressions and multiple matching support for regular expressions.

Well, that's almost it, if there is something wrong, you can point it out in the comment section, and make progress together! (•̀ ω • ́ )✧

(By the way, how to use regularity in MySQL? It seems that just add the keyword REGEXP, right?...)

Guess you like

Origin blog.csdn.net/qq_44386537/article/details/112107299