Table of Contents
Matches a single character
Use quantifiers
Greedy matching
Lazy matching
Capture grouping (similar to macro definition )
"OR" logic
"not" logic with parentheses
Simple pattern matching
Boundary
Start and end of line
Word boundaries and non-word boundaries
Unicode characters and other characters
Home Backend Development PHP Tutorial Summary of regular expression characters

Summary of regular expression characters

Feb 22, 2018 pm 01:14 PM
character expression

Basic regular expression

Matches a single character

Matches the writing of a single number, which can be "[0-9]" or " \d”.

matches a single non-numeric character , then use uppercase "\D".

Matches any and of the 26 letters, use "[a-zA-Z]"

Matches any one character, use the period If "."

matches specific characters, just write it directly. For example, "abcd" matches itself. If you encounter special characters, you need to escape , and the escape character is "\</code>".

matches a character and the use of square brackets is called "character set". Square brackets are used to specify a "set", matching a character in this set, such as the hexadecimal number "[0-9a-fA-F]". The dot in the character set represents the dot itself , but other special characters still need to be transferred, such as the backslash character.

Use quantifiers

Greedy matching

If you want to express the repetition of a rule, you need to use quantifiers. Use curly braces to indicate the number of repetitions. For example, 8 numbers can be expressed like this: "\d{8}"

The quantifiers in the curly brackets can be changed. For example, if 7 to 8 numbers are expressed, it is expressed as " \d{7,8}". The rvalue representing the upper limit does not need to be written. For example, "{0,}" is legal, indicating that it is greater than or equal to 0 characters; but "{,10}" is trying to express the upper limit alone. ” is illegal and should at least be written as “{0,10}”.

The plus sign "+" indicates that the number of elements to its left is "one or more", which is equal to the effect of "{1,}". So the plus sign is also a special character.

The asterisk "*" means that the number of elements to its left is "zero or at least one", that is, "{0,}".

The question mark "?" means "zero or one", which is equivalent to "{0,1}".

Lazy matching

The above items such as + and * will use the "greedy" pattern when matching. That is, match as many numbers as possible. For example, if you use "5+" to match the string "55555", it will match the longest string it can find, which is "55555".

If you add a question mark after the quantifier, the matching pattern will become "lazy", which is the one with the least matching. For example, if you use "5+?" to match, you will only find the smallest matching character "5".

The following are available lazy matching expressions: +? , *?, {n,}?, {m,n}?

Capture grouping (similar to macro definition )

You can "capture" part of the expression and reference it later as a macro. Use brackets to define (capture), and then use "\1" after the definition for reference; if it is the second capture, use "\2", and so on.

Groups are generally saved, but when the expression is very long, it may be necessary to explicitly indicate not to save the group. For example, if you use the format "(?:THE|The|the)", you use the "?:" label to indicate that no naming tags are required.

"OR" logic

Use "|" to link two fields to provide "OR" logic. Note the use of

"not" logic with parentheses

If the character "^" is used in the set "[...]", It means "not", for example, "[^0-9]" is equivalent to "\D".

Summary of regular expression characters

Simple pattern matching

The following is a list of commonly used single character matches:

##Number##Letters, numbers, underscores\w[_a-zA-Z0-9]##non-digitNon-letter\t\0##Backspace[\b] is equivalent to "”\bThis only matches the beginning/end of the word, no characters are consumedAny characterThe line terminator cannot be matched using this symbol

Summary of regular expression characters

Boundary

This section designs a concept: Assertion, also known as "Zero-width assertion ( zero-width assertion)". This concept does not match characters, but positions in the string.

Start and end of line

  • Use "^" to indicate the beginning of a line

  • Use "$" to indicate the end of a line

Word boundaries and non-word boundaries

For example, to match the word "the", write "\bthe\b”. If you want to match words with "e" in the middle of "brother", you can write "\Be\B"

You can use "\<" to match the beginning of the word, " \>” matches the end of a word. However, these two are not recommended because new matchers may not support them.

Summary of regular expression characters

Unicode characters and other characters

Regular expressions support inputting unicode values, such as “\u00e9". Note that unicode must have four hexadecimal digits, either upper or lower case. Javascript also supports "\xe9", but "\x00e9" is wrong.

Related recommendations:

js regular expression verification time format example

Regular expression \v metacharacter detailed explanation

JS regular expression key points analysis


Reference type Pattern Remarks
\d
Equivalent to ""
\D
\W
##Tab characterTab
##Null character

##Space
\s
[ \t\n\r] ##Return \r
Line break \n
##Space between words
.

The above is the detailed content of Summary of regular expression characters. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use java's Character.isDigit() function to determine whether a character is a number Use java's Character.isDigit() function to determine whether a character is a number Jul 27, 2023 am 09:32 AM

Use Java's Character.isDigit() function to determine whether a character is a numeric character. Characters are represented in the form of ASCII codes internally in the computer. Each character has a corresponding ASCII code. Among them, the ASCII code values ​​corresponding to the numeric characters 0 to 9 are 48 to 57 respectively. To determine whether a character is a number, you can use the isDigit() method provided by the Character class in Java. The isDigit() method is of the Character class

How to type arrows in Word How to type arrows in Word Apr 16, 2023 pm 11:37 PM

How to use AutoCorrect to type arrows in Word One of the fastest ways to type arrows in Word is to use the predefined AutoCorrect shortcuts. If you type a specific sequence of characters, Word automatically converts those characters into arrow symbols. You can draw many different arrow styles using this method. To type an arrow in Word using AutoCorrect: Move your cursor to the location in the document where you want the arrow to appear. Type one of the following character combinations: If you don't want what you type to be corrected to an arrow symbol, press the backspace key on your keyboard to

How to apply superscript and subscript formatting options in Microsoft Excel How to apply superscript and subscript formatting options in Microsoft Excel Apr 14, 2023 pm 12:07 PM

A superscript is a character or characters, either letters or numbers, that you need to set slightly above the normal line of text. For example, if you need to write 1st, the letter st needs to be slightly higher than the character 1. Likewise, a subscript is a group of characters or a single character and needs to be set slightly lower than normal text level. For example, when you write a chemical formula, you need to place the numbers below the normal line of characters. The following screenshots show some examples of superscript and subscript formatting. Although it may seem like a daunting task, applying superscript and subscript formatting to your text is actually quite simple. In this article, we will explain in some simple steps how to easily format text using superscript or subscript. Hope you enjoyed reading this article. How to apply superscript in Excel

How do you enter extended characters, such as the degree symbol, on iPhone and Mac? How do you enter extended characters, such as the degree symbol, on iPhone and Mac? Apr 22, 2023 pm 02:01 PM

Your physical or numeric keyboard provides a limited number of character options on the surface. However, there are several ways to access accented letters, special characters, and more on iPhone, iPad, and Mac. The standard iOS keyboard gives you quick access to uppercase and lowercase letters, standard numbers, punctuation, and characters. Of course, there are many other characters. You can choose from letters with diacritics to upside-down question marks. You may have stumbled upon a hidden special character. If not, here's how to access them on iPhone, iPad, and Mac. How to Access Extended Characters on iPhone and iPad Getting extended characters on your iPhone or iPad is very simple. In "Information", "

Correct way to display Chinese characters in matplotlib Correct way to display Chinese characters in matplotlib Jan 13, 2024 am 11:03 AM

Correctly displaying Chinese characters in matplotlib is a problem often encountered by many Chinese users. By default, matplotlib uses English fonts and cannot display Chinese characters correctly. To solve this problem, we need to set the correct Chinese font and apply it to matplotlib. Below are some specific code examples to help you display Chinese characters correctly in matplotlib. First, we need to import the required libraries: importmatplot

How to use Golang to determine whether a character is a letter How to use Golang to determine whether a character is a letter Dec 23, 2023 am 11:57 AM

How to use Golang to determine whether a character is a letter. In Golang, determining whether a character is a letter can be achieved by using the IsLetter function in the Unicode package. The IsLetter function checks whether the given character is a letter. Next, we will introduce in detail how to use Golang to write code to determine whether a character is a letter. First, you need to create a new Go file in which to write the code. You can name the file "main.go". code

Regarding the character representation of the Enter key in Java, which one is it? Regarding the character representation of the Enter key in Java, which one is it? Mar 29, 2024 am 11:48 AM

The character representation of the Enter key in Java is `. In Java, ` represents a newline character, and when this character is encountered, the text output will wrap. Here is a simple code example that demonstrates how to use `` to represent the Enter key: publicclassMain{publicstaticvoidmain(String[]args){System.out.println("This is the first line of this

How to type special characters on Windows 11 How to type special characters on Windows 11 Apr 17, 2023 pm 08:28 PM

Enable touch keyboard in tablet mode If you have a touch screen laptop, you can use the touch keyboard to type multiple special characters on Windows 11. This is probably the easiest way to add special characters. Enable special characters for touchscreen on Windows 11: Open the Start menu and select Settings. When Settings opens, navigate to Time & Language > Typing > Touch Keyboard. In the Typing menu, check the "Show touch keyboard when no keyboard is available" option. Enable touch keyboard without tablet mode Another way to access the touch keyboard is to make it appear on the taskbar full-time. To make the touch keyboard accessible, you need to tell Windows 11 to display it. Use the following steps: From the Start menu, select

See all articles