The following editor will bring you a detailed application explanation of the basic grammar of regular expressions (a must-read article). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
1. Basic syntax of regular expressions
Two special symbols '^' and '$ '. Their function is to indicate the beginning and end of a string respectively. Examples are as follows:
"^The": indicates all strings starting with "The" ("There", "The cat", etc.);
"of despair$": indicates so A string ending with "of despair";
"^abc$": Indicates a string that starts and ends with "abc" - haha, there is only "abc" itself;
"notice": Represents any string containing "notice".
Like the last example, if you don't use two special characters, you are indicating that the string you want to find is within any part of the string being searched for - you are not positioning it at the top.
Other symbols include '*', '+' and '?', which represent the number of times a character or a sequence of characters appears repeatedly. They mean "none or more", "once or more" and "none or once" respectively. Here are a few examples:
"ab*": Indicates that a string has an a followed by zero or several b. ("a", "ab", "abbb",...);
"ab+": Indicates that a string has an a followed by at least one b or more;
" ab?": Indicates that a string has an a followed by zero or one b;
"a?b+$": Indicates that there is zero or one a followed by one or several b at the end of the string .
You can also use a range, enclosed in curly brackets, to indicate the range of repetitions.
"ab{2}": Indicates that a string has an a followed by 2 b ("abb");
"ab{2,}": Indicates that a string has a a followed by at least 2 b;
"ab{3,5}": Indicates that a string has an a followed by 3 to 5 b.
Please note that you must specify the lower limit of the range (eg: "{0,2}" instead of "{,2}"). Also, you may have noticed that '*', '+' and '?' are equivalent to "{0,}", "{1,}" and "{0,1}".
There is also a '¦', which means "or" operation:
"hi¦hello": indicates that there is "hi" or "hello" in a string;
"(b¦cd)ef": represents "bef" or "cdef";
"(a¦b)*c": represents a string of mixed "a" and "b" followed by A "c";
'.' can replace any character:
"a.[0-9]": Indicates that a string has an "a" followed by an arbitrary character and A number;
"^.{3}$": indicates a string of any three characters (length is 3 characters);
The square brackets indicate that certain characters are allowed in a Appears at a specific position in the string:
"[ab]": indicates that a string has an "a" or "b" (equivalent to "a¦b");
"[a-d]": Indicates that a string contains one of lowercase 'a' to 'd' (equivalent to "a¦b¦c¦d" or "[abcd]");
"^[a-zA-Z]": represents a string starting with a letter;
"[0-9]%": represents a number preceded by a percent sign;
",[a-zA-Z0-9]$": Indicates that a string ends with a comma followed by a letter or number.
You can also use '^' in square brackets to indicate unwanted characters. '^' should be the first one in the square brackets. (For example: "%[^a-zA-Z]%" means that letters should not appear between two percent signs).
In order to express verbatim, you must add the transfer character '\' before the characters "^.$()¦*+?{\".
Please note that within square brackets, no escape characters are required.
2. Regular expression verification controls the input character type of the text box
1. Only numbers and English can be entered:
2. Only numbers can be entered:
3. Only full-width characters can be entered:
4. Only Chinese characters can be entered:
3 , Popular explanation of application examples of regular expressions
//Verify whether it is composed entirely of numbers
/^[0-9]{1,20}$/
^ means that the leading character should match the rule immediately following ^
$ means that the leading character should match the rule immediately preceding $
[ ] The content is Optional character set
[0-9] means the required character range is between 0-9
{1,20} means the legal length of the numeric string is 1 to 20, which is [ The number of occurrences of characters in 0-9] ranges from 1 to 20 times.
/^ and $/ should be used in pairs to indicate that the entire string is required to completely match the defined rule, rather than only matching a substring in the string.
//Verify login name: You can only enter 5-20 strings starting with letters and including numbers, "_", and "."
/^[a-zA -Z]{1}([a-zA-Z0-9]|[._]){4,19}$/
^[a-zA-Z]{1} means the first one Character requirements are letters.
([a-zA-Z0-9]|[._]){4,19} means starting from the second digit (because it immediately follows the previous expression) with a length of 4 A string of up to 9 characters, which must be composed of uppercase and lowercase letters, numbers, or the special character set [._].
//Verify user name: Only 1-30 strings starting with letters can be entered
/^[a-zA-Z]{1,30}$/
//Verify password: Only 6-20 letters, numbers, and underscores can be entered
/^(\w){6,20}$/
\w: Used to match letters, numbers or underline characters
//Verify ordinary telephone and fax numbers: it can start with "+" or numbers, and can contain "-" and " "
/^[ +]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/
\d : Used to match numbers from 0 to 9;
The "?" metacharacter specifies that its leading object must appear zero or once in the target object
Strings that can be matched such as: +123 -999 999; +123-999 999; 123 999 999; +123 999999, etc.
//Verify URL
/^http[s]{0,1}:\ /\/.+$/ or /^http[s]{0,1}:\/\/.{1,n}$/ (indicates that the length of the url string is length(“https://”) + n )
\ /: Represents the character "/".
. Represents the set of all characters
+ is equivalent to {1,}, which is from 1 to positive infinity.
4. Regular expression application (common parts)
##"^\d+$" //Non-negative integer (positive Integer + 0) "^[0-9]*[1-9][0-9]*$" // Positive integer "^((-\d+)| (0+))$" //Non-positive integer (negative integer + 0) "^-[0-9]*[1-9][0-9]*$" //Negative integer "^-?\d+$" //Integer "^\d+(\.\d+)?$" //Non-negative floating point number (positive floating point number + 0)"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0 -9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$" //Positive floating point number"^( (-\d+(\.\d+)?)|(0+(\.0+)?))$" //Non-positive floating point number (negative floating point number + 0) "^(- (([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[ 0-9]+)|([0-9]*[1-9][0-9]*)))$" //Negative floating point number "^(-?\d+)( \.\d+)?$" //Floating point number"^[A-Za-z]+$" //A string consisting of 26 English letters"^[ A-Z]+$" //A string consisting of 26 uppercase English letters"^[a-z]+$" //A string consisting of 26 lowercase English letters"^[A-Za-z0-9]+$" // A string consisting of numbers and 26 English letters "^\w+$" // A string consisting of numbers, 26 English letters or String composed of underscores"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$" //email address"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\ ?\S*)?$" //url/^(d{2}|d{4})-((0([1-9]{1}))|(1[1 |2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/ // Year-month-day/ ^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0| 1]))/(d{2}|d{4})$/ // Month/Day/Year"^([w-.]+)@(([[0-9] {1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z] {2,4}|[0-9]{1,3})(]?)$" //Emil"(d+-)?(d{4}-?d{7}| d{3}-?d{8}|^d{7,8})(-d+)?" //Phone number"^(d{1,2}|1dd|2[0 -4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2 [0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$" //IP address^([0-9A-F]{2})(-[0-9A-F]{2}){5}$ //Regular expression of MAC address^[-+ ]?\d+(\.\d+)?$ //Value type regular expressionThe above is the detailed content of Tutorial on basic syntax of regular expressions. For more information, please follow other related articles on the PHP Chinese website!

提到API开发,你可能会想到DjangoRESTFramework,Flask,FastAPI,没错,它们完全可以用来编写API,不过,今天分享的这个框架可以让你更快把现有的函数转化为API,它就是Sanic。Sanic简介Sanic[1],是Python3.7+Web服务器和Web框架,旨在提高性能。它允许使用Python3.5中添加的async/await语法,这可以有效避免阻塞从而达到提升响应速度的目的。Sanic致力于提供一种简单且快速,集创建和启动于一体的方法

随着PHP8.0的发布,新增了一种类型别名语法,使得使用自定义的类型变得更加容易。在本文中,我们将深入了解这种新的语法,以及它对开发人员的影响。什么是类型别名?在PHP中,类型别名本质上是一个变量,它引用另一个类型的名称。这个变量可以像其他类型一样使用,并在代码中的任何地方声明。这种语法的主要作用是为常用的类型定义自定义别名,使得代码更加易于阅读和理解。

Go语言与JS的联系与区别Go语言(也称为Golang)和JavaScript(JS)都是当前流行的编程语言,它们在某些方面有联系,在其他方面又有明显的区别。本篇文章将探讨Go语言与JavaScript之间的联系与区别,同时提供具体的代码示例来帮助读者更好地理解这两种编程语言。联系:都是跨平台的Go语言和JavaScript都是跨平台的,可以在不同的操作系统

PHP是一种广泛应用于Web开发的服务器端脚本语言,而PHP8.0版本中引入了一种新的父类调用语法,让面向对象编程更加方便和简洁。在PHP中,我们可以通过继承的方式创建一个父类和一个或多个子类。子类可以继承父类的属性和方法,并可以通过重写父类的方法来修改或扩展其功能。在普通的PHP继承中,如果我们想在子类中调用父类的方法,需要使用parent关键字来引用父

掌握基本的CSS选择器语法,需要具体代码示例CSS选择器是前端开发中非常重要的一部分,它可以用来选择和修改HTML文档的各个元素。掌握基本的CSS选择器语法对于编写高效的样式表是至关重要的。本文将介绍一些常见的CSS选择器以及对应的代码示例。元素选择器元素选择器是最基本的选择器,可以通过元素的标签名来选择对应的元素。例如,要选择所有的段落(p元素),可以使用

Python作为一种高级编程语言,易于学习和使用。一旦需要编写Python程序时,无法避免地遇到语法错误,表达式语法错误是常见的一种。在本文中,我们将讨论如何解决Python的表达式语法错误。表达式语法错误是Python中最常见的错误之一,它通常是由于错误的使用语法或缺少必要组件而导致的。在Python中,表达式通常由数字、字符串、变量和运算符组成。最常见的

C语言中乘方运算的语法和用法简介:在C语言中,乘方运算(poweroperation)是一种常见的数学运算,它用于计算一个数的幂。在C语言中,我们可以使用标准库函数或者自定义函数来实现乘方运算。本文将详细介绍C语言中乘方运算的语法和用法,并提供具体的代码示例。一、使用math.h中的pow()函数在C语言中,math.h标准库中提供了pow()函数,用于执

随着PHP技术的不断发展,PHP8.0带来了一系列的新特性和功能,其中依赖注入的使用方法也得到了进一步的创新和完善。本文将为大家介绍PHP8.0中依赖注入的语法,让您能够更好地掌握PHP技术的最新进展。什么是依赖注入先来简单介绍一下依赖注入是什么。依赖注入(DependencyInjection)是一种编程技术,它主要用于降低代码的耦合程


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.