.
" > 2.1 点运算符 .
$
号" > 2.8.2 $
号
(...)
特征标群" > 2.5 (...)
特征标群
|
或运算符" > 2.6 |
或运算符
?=...
正先行断言" > 4.1 ?=...
正先行断言
?!...
负先行断言" > 4.2 ?!...
负先行断言
?<= ...
正后发断言" > 4.3 ?<= ...
正后发断言
? 负后发断言" >? 负后发断言
4.4 Regular expression syntax tutorial (including online testing tool)
What is a regular expression?
A regular expression is a group of letters and symbols special text, which can be used to find sentences that meet the format you want from the text.
A regular expression is when matching strings from left to right in a body string a style of. The term "Regular expression" is a bit tricky to pronounce, and we often use the abbreviated terms "regex" or "regexp". Regular expressions can replace strings in text according to certain matching patterns from a basic string, validate forms, extract strings, etc.
Imagine you are writing an application, and then you want to set an User naming rules allow user names to contain characters, numbers, underscores and hyphens, and limit the number of characters so that the name does not look so ugly. We use the following regular expression to verify a username:
The above regular expression can accept john_doe
, jo-hn_doe
, john12_as
.
But it does not match Jo
, because it contains uppercase letters and is too short.
Directory
2.5 (...) Characteristic group
2.7 Convert special characters
# 3. Abbreviated character set
4. Zero-width assertion (before and after preview)
4.1 ?=...Positive lookahead assertion
4.2 ?!... Negative lookahead assertion
5.2 Global search(Global search)
5.3 Multiline modifier (Multiline)
Regular expression actually This is the format used when performing a search, and is a combination of letters and numbers. For example: a regular expression the, which represents a rule: starting with the letters
t, followed by
Metacharacters | Description |
---|---|
. | Period matches any single character except newline. |
[ ] | Character type. Matches any character within square brackets. |
[^ ] | Negative character type. Matches any characters except square brackets |
* | ## Matches >= 0 repeated characters before the * sign.|
Match >= 1 repeated character before the number. | |
Mark? The preceding characters are optional. | |
Matches num characters before the braces (n <= num <= m). | |
Character set, matching strings that are exactly equal to xyz. | |
or operator, matches the characters before or after the symbol. | |
escape character, used to match some reserved characters | [ ] ( ) { } . * ? ^ $ \ |
|
Match from the beginning line. | |
Match from the end. |
简写 | 描述 |
---|---|
. | 除换行符外的所有字符 |
\w | 匹配所有字母数字, 等同于 [a-zA-Z0-9_] |
\W | 匹配所有非字母数字, 即符号, 等同于: [^\w] |
\d | 匹配数字: [0-9] |
\D | 匹配非数字: [^\d] |
\s | 匹配所有空格字符, 等同于: [\t\n\f\r\p{Z}] |
\S | 匹配所有非空格字符: [^\s] |
\f | 匹配一个换页符 |
\n | 匹配一个换行符 |
\r | 匹配一个回车符 |
\t | 匹配一个制表符 |
\v | 匹配一个垂直制表符 |
\p | 匹配 CR/LF (等同于 \r\n ),用来匹配 DOS 行终止符 |
4. 零宽度断言(前后预查)
先行断言和后发断言都属于非捕获簇(不捕获文本 ,也不针对组合计进行计数). 先行断言用于判断所匹配的格式是否在另一个确定的格式之前, 匹配结果不包含该确定格式(仅作为约束).
例如, 我们想要获得所有跟在 $
符号后的数字, 我们可以使用正后发断言 (?<=\$)[0-9\.]*
.
这个表达式匹配 $
开头, 之后跟着 0,1,2,3,4,5,6,7,8,9,.
这些字符可以出现大于等于 0 次.
零宽度断言如下:
符号 | 描述 |
---|---|
?= | 正先行断言-存在 |
?! | 负先行断言-排除 |
?<= | 正后发断言-存在 |
? | 负后发断言-排除 |
?=...
正先行断言
4.1 ?=...
正先行断言, 表示第一部分表达式之后必须跟着 ?=...
定义的表达式.
返回结果只包含满足匹配条件的第一部分表达式.
定义一个正先行断言要使用 ()
. 在括号内部使用一个问号和等号: (?=...)
.
正先行断言的内容写在括号中的等号后面.
例如, 表达式 (T|t)he(?=\sfat)
匹配 The
和 the
, 在括号中我们又定义了正先行断言 (?=\sfat)
,即 The
和 the
后面紧跟着 (空格)fat
.
"(T|t)he(?=\sfat)" => The fat cat sat on the mat.
?!...
负先行断言
4.2 负先行断言 ?!
用于筛选所有匹配结果, 筛选条件为 其后不跟随着断言中定义的格式.正先行断言
定义和 负先行断言
一样, 区别就是 =
替换成 !
也就是 (?!...)
.
表达式 (T|t)he(?!\sfat)
匹配 The
和 the
, 且其后不跟着 (空格)fat
.
"(T|t)he(?!\sfat)" => The fat cat sat on the mat.
?<= ...
正后发断言
4.3 正后发断言 记作(?<=...)
用于筛选所有匹配结果, 筛选条件为 其前跟随着断言中定义的格式.
例如, 表达式 (?<=(T|t)he\s)(fat|mat)
匹配 fat
和 mat
, 且其前跟着 The
或 the
.
"(?<=(T|t)he\s)(fat|mat)" => The fat cat sat on the mat.
?<!...
负后发断言
4.4 负后发断言 记作 (?<!...)
用于筛选所有匹配结果, 筛选条件为 其前不跟随着断言中定义的格式.
例如, 表达式 (?<!(T|t)he\s)(cat)
匹配 cat
, 且其前不跟着 The
或 the
.
"(?<!(T|t)he\s)(cat)" => The cat sat on cat.
5. Flags
Flags are also called pattern modifiers, because they can be used to modify the search results of expressions. These flags can be used in any combination and are part of the entire regular expression.
Flags | Description |
---|---|
i | Ignore case. |
g | Global search. |
m | Multiple lines: Anchor metacharacter^ $ The working range is at the beginning of each line. |
5.1 忽略大小写 (Case Insensitive)
修饰语 i
用于忽略大小写.
例如, 表达式 /The/gi
表示在全局搜索 The
, 在后面的 i
将其条件修改为忽略大小写, 则变成搜索 the
和 The
, g
表示全局搜索.
"the" => The fat cat sat on the mat.
"/The/gi" => The fat cat sat on the mat.
5.2 全局搜索 (Global search)
修饰符 g
常用于执行一个全局搜索匹配, 即(不仅仅返回第一个匹配的, 而是返回全部).
例如, 表达式 /.(at)/g
表示搜索 任意字符(除了换行) + at
, 并返回全部结果.
"/.(at)/" => The fat cat sat on the mat.
"/.(at)/g" => The fat cat sat on the mat.
5.3 多行修饰符 (Multiline)
多行修饰符 m
常用于执行一个多行匹配.
像之前介绍的 (^,$)
用于检查格式是否是在待检测字符串的开头或结尾. 但我们如果想要它在每行的开头和结尾生效, 我们需要用到多行修饰符 m
.
例如, 表达式 /at(.)?$/gm
表示小写字符 a
后跟小写字符 t
, 末尾可选除换行符外任意字符. 根据 m
修饰符, 现在表达式匹配每行的结尾.
"/.at(.)?$/" => The fat cat sat on the mat.
"/.at(.)?$/gm" => The fat cat sat on the mat.
6. 贪婪匹配与惰性匹配 (Greedy vs lazy matching)
正则表达式默认采用贪婪匹配模式,在该模式下意味着会匹配尽可能长的子串。我们可以使用 ?
将贪婪匹配模式转化为惰性匹配模式。
"/(.*at)/" => The fat cat sat on the mat.
"/(.*?at)/" => The fat cat sat on the mat.
相关推荐:
2. Regular expression video tutorial
3.Regular expression is a manual download
4. Regular expression online testing tool

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP regular expression verification: Number format detection When writing PHP programs, it is often necessary to verify the data entered by the user. One of the common verifications is to check whether the data conforms to the specified number format. In PHP, you can use regular expressions to achieve this kind of validation. This article will introduce how to use PHP regular expressions to verify number formats and provide specific code examples. First, let’s look at common number format validation requirements: Integers: only contain numbers 0-9, can start with a plus or minus sign, and do not contain decimal points. floating point

To validate email addresses in Golang using regular expressions, follow these steps: Use regexp.MustCompile to create a regular expression pattern that matches valid email address formats. Use the MatchString function to check whether a string matches a pattern. This pattern covers most valid email address formats, including: Local usernames can contain letters, numbers, and special characters: !.#$%&'*+/=?^_{|}~-`Domain names must contain at least One letter, followed by letters, numbers, or hyphens. The top-level domain (TLD) cannot be longer than 63 characters.

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

As a modern programming language, Go language provides powerful regular expressions and string processing functions, allowing developers to process string data more efficiently. It is very important for developers to master regular expressions and string processing in Go language. This article will introduce in detail the basic concepts and usage of regular expressions in Go language, and how to use Go language to process strings. 1. Regular expressions Regular expressions are a tool used to describe string patterns. They can easily implement operations such as string matching, search, and replacement.

PHP Regular Expressions: Exact Matching and Exclusion Fuzzy inclusion regular expressions are a powerful text matching tool that can help programmers perform efficient search, replacement and filtering when processing text. In PHP, regular expressions are also widely used in string processing and data matching. This article will focus on how to perform exact matching and exclude fuzzy inclusion operations in PHP, and will illustrate it with specific code examples. Exact match Exact match means matching only strings that meet the exact condition, not any variations or extra words.

The method of using regular expressions to verify passwords in Go is as follows: Define a regular expression pattern that meets the minimum password requirements: at least 8 characters, including lowercase letters, uppercase letters, numbers, and special characters. Compile regular expression patterns using the MustCompile function from the regexp package. Use the MatchString method to test whether the input string matches a regular expression pattern.

The steps to detect URLs in Golang using regular expressions are as follows: Compile the regular expression pattern using regexp.MustCompile(pattern). Pattern needs to match protocol, hostname, port (optional), path (optional) and query parameters (optional). Use regexp.MatchString(pattern,url) to detect whether the URL matches the pattern.

Regular expression wildcards include ".", "*", "+", "?", "^", "$", "[]", "[^]", "[a-z]", "[A-Z] ","[0-9]","\d","\D","\w","\W","\s&quo