Home> headlines> body text

Regular expression syntax tutorial (including online testing tool)

angryTom
Release: 2019-08-23 10:57:31
Original
10187 people have browsed it

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:

Regular expression syntax tutorial (including online testing tool)

The above regular expression can acceptjohn_doe,jo-hn_doe,john12_as. But it does not matchJo, because it contains uppercase letters and is too short.

Directory

1. Basic matching

2. Metacharacters

2.1 Dot operator.

2.2 Character set

2.2.1 Negative character set

2.3 Number of repetitions

2.3. #1 }

2.5 (...) Characteristic group

2.6 | Or operator

2.7 Convert special characters

2.8 Anchor point

#2.8.2 $

# 3. Abbreviated character set

4. Zero-width assertion (before and after preview)

4.1 ?=...Positive lookahead assertion

4.2 ?!... Negative lookahead assertion

4.3 ?

4.4 ?

5. Flag

5.1 Case Insensitive

5.2 Global search(Global search)

5.3 Multiline modifier (Multiline)

##1. Basic matching

Regular expression actually This is the format used when performing a search, and is a combination of letters and numbers. For example: a regular expressionthe, which represents a rule: starting with the letters

t

, followed by

Online testing tool

Regular expression123matches the string123. It matches the entered regular expression character by character Do comparison.

Regular expressions are case-sensitive, soThewill not matchthe.

"The" => The fat cat sat on the mat.
Copy after login
Copy after login

Online testing tool

2. Metacharacters

Regular expressions mainly rely on metacharacters. Metacharacters do not represent their literal meaning, they all have special meanings. Some metacharacters have special meanings when written in square brackets. The following is an introduction to some metacharacters:

2.1 点运算符.

  .是元字符中最简单的例子..匹配任意单个字符, 但不匹配换行符. 例如, 表达式.ar匹配一个任意字符后面跟着是ar的字符串.

".ar" => The car parked in the garage.
Copy after login

在线测试工具

2.2 字符集

  字符集也叫做字符类. 方括号用来指定一个字符集. 在方括号中使用连字符来指定字符集的范围. 在方括号中的字符集不关心顺序. 例如, 表达式[Tt]he匹配theThe.

"[Tt]he" => The car parked in the garage.
Copy after login

在线测试工具

  方括号的句号就表示句号. 表达式ar[.]匹配ar.字符串

"ar[.]" => A garage is a good place to park a car.
Copy after login

在线测试工具

2.2.1 否定字符集

  一般来说^表示一个字符串的开头, 但它用在一个方括号的开头的时候, 它表示这个字符集是否定的. 例如, 表达式[^c]ar匹配一个后面跟着ar的除了c的任意字符.

"[^c]ar" => The car parked in the garage.
Copy after login

在线测试工具

2.3 重复次数

  后面跟着元字符+,*or?的, 用来指定匹配子模式的次数. 这些元字符在不同的情况下有着不同的意思.

2.3.1*

  *号匹配 在*之前的字符出现大于等于0次. 例如, 表达式a*匹配以0或更多个a开头的字符, 因为有0个这个条件, 其实也就匹配了所有的字符. 表达式[a-z]*匹配一个行中所有以小写字母开头的字符串.

"[a-z]*" => The car parked in the garage #21.
Copy after login

在线测试工具

  *字符和.字符搭配可以匹配所有的字符.*.*和表示匹配空格的符号\s连起来用, 如表达式\s*cat\s*匹配0或更多个空格开头和0或更多个空格结尾的cat字符串.

"\s*cat\s*" => The fat cat sat on the concatenation.
Copy after login

在线测试工具

2.3.2+

  +号匹配+号之前的字符出现 >=1 次. 例如表达式c.+t匹配以首字母c开头以t结尾,中间跟着任意个字符的字符串.

"c.+t" => The fat cat sat on the mat.
Copy after login

在线测试工具

2.3.3?

  在正则表达式中元字符?标记在符号前面的字符为可选, 即出现 0 或 1 次. 例如, 表达式[T]?he匹配字符串heThe.

"[T]he" => The car is parked in the garage.
Copy after login

在线测试工具

"[T]?he" => The car is parked in the garage.
Copy after login

在线测试工具

2.4{}

  在正则表达式中{}是一个量词, 常用来一个或一组字符可以重复出现的次数. 例如, 表达式[0-9]{2,3}匹配最少 2 位最多 3 位 0~9 的数字.

"[0-9]{2,3}" => The number was 9.9997 but we rounded it off to 10.0.
Copy after login

在线测试工具

  我们可以省略第二个参数. 例如,[0-9]{2,}匹配至少两位 0~9 的数字.

"[0-9]{2,}" => The number was 9.9997 but we rounded it off to 10.0.
Copy after login

在线测试工具

  如果逗号也省略掉则表示重复固定的次数. 例如,[0-9]{3}匹配3位数字

"[0-9]{3}" => The number was 9.9997 but we rounded it off to 10.0.
Copy after login

在线测试工具

2.5(...)特征标群

  特征标群是一组写在(...)中的子模式. 例如之前说的{}是用来表示前面一个字符出现指定次数. 但如果在{}前加入特征标群则表示整个标群内的字符重复 N 次. 例如, 表达式(ab)*匹配连续出现 0 或更多个ab.

  我们还可以在()中用或字符|表示或. 例如,(c|g|p)ar匹配cargarpar.

"(c|g|p)ar" => The car is parked in the garage.
Copy after login

在线测试工具

2.6|或运算符

  或运算符就表示或, 用作判断条件.

  例如(T|t)he|car匹配(T|t)hecar.

"(T|t)he|car" => The car is parked in the garage.
Copy after login

在线测试工具

2.7 转码特殊字符

  反斜线\在表达式中用于转码紧跟其后的字符. 用于指定{ } [ ] / \ + * . $ ^ | ?这些特殊字符. 如果想要匹配这些特殊字符则要在其前面加上反斜线\.

  例如.是用来匹配除换行符外的所有字符的. 如果想要匹配句子中的.则要写成\.以下这个例子\.?是选择性匹配.

"(f|c|m)at\.?" => The fat cat sat on the mat.
Copy after login

在线测试工具

2.8 锚点

  在正则表达式中, 想要匹配指定开头或结尾的字符串就要使用到锚点.^指定开头,$指定结尾.

2.8.1^

  ^用来检查匹配的字符串是否在所匹配字符串的开头.

  例如, 在abc中使用表达式^a会得到结果a. 但如果使用^b将匹配不到任何结果. 因为在字符串abc中并不是以b开头.

  例如,^(T|t)he匹配以Thethe开头的字符串.

"(T|t)he" => The car is parked in the garage.
Copy after login

在线测试工具

"^(T|t)he" => The car is parked in the garage.
Copy after login

在线测试工具

2.8.2$

  同理于^号,$号用来匹配字符是否是最后一个.

  例如,(at\.)$匹配以at.结尾的字符串.

"(at\.)" => The fat cat. sat. on the mat.
Copy after login

在线测试工具

"(at\.)$" => The fat cat. sat. on the mat.
Copy after login

在线测试工具

3. 简写字符集

  正则表达式提供一些常用的字符集简写. 如下:

## Matches >= 0 repeated characters before the * sign. Match >= 1 repeated character before the number. ? Mark? The preceding characters are optional. {n,m} Matches num characters before the braces (n <= num <= m). (xyz) 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.
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
*
[ ] ( ) { } . * ? ^ $ \ |
简写 描述
. 除换行符外的所有字符
\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)匹配Thethe, 在括号中我们又定义了正先行断言(?=\sfat),即Thethe后面紧跟着(空格)fat.

"(T|t)he(?=\sfat)" => The fat cat sat on the mat.
Copy after login

在线测试工具

4.2?!...负先行断言

  负先行断言?!用于筛选所有匹配结果, 筛选条件为 其后不跟随着断言中定义的格式.正先行断言定义和负先行断言一样, 区别就是=替换成!也就是(?!...).

  表达式(T|t)he(?!\sfat)匹配Thethe, 且其后不跟着(空格)fat.

"(T|t)he(?!\sfat)" => The fat cat sat on the mat.
Copy after login

在线测试工具

4.3?<= ...正后发断言

  正后发断言 记作(?<=...)用于筛选所有匹配结果, 筛选条件为 其前跟随着断言中定义的格式. 例如, 表达式(?<=(T|t)he\s)(fat|mat)匹配fatmat, 且其前跟着Thethe.

"(?<=(T|t)he\s)(fat|mat)" => The fat cat sat on the mat.
Copy after login

在线测试工具

4.4?负后发断言

  负后发断言 记作(?用于筛选所有匹配结果, 筛选条件为 其前不跟随着断言中定义的格式. 例如, 表达式(?匹配cat, 且其前不跟着Thethe.

"(? The cat sat on cat.
Copy after login

在线测试工具

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将其条件修改为忽略大小写, 则变成搜索theThe,g表示全局搜索.

"The" => The fat cat sat on the mat.
Copy after login
Copy after login

在线测试工具

"/The/gi" => The fat cat sat on the mat.
Copy after login

在线测试工具

5.2 全局搜索 (Global search)

  修饰符g常用于执行一个全局搜索匹配, 即(不仅仅返回第一个匹配的, 而是返回全部). 例如, 表达式/.(at)/g表示搜索 任意字符(除了换行) +at, 并返回全部结果.

"/.(at)/" => The fat cat sat on the mat.
Copy after login

在线测试工具

"/.(at)/g" => The fat cat sat on the mat.
Copy after login

在线测试工具

5.3 多行修饰符 (Multiline)

  多行修饰符m常用于执行一个多行匹配.

  像之前介绍的(^,$)用于检查格式是否是在待检测字符串的开头或结尾. 但我们如果想要它在每行的开头和结尾生效, 我们需要用到多行修饰符m.

  例如, 表达式/at(.)?$/gm表示小写字符a后跟小写字符t, 末尾可选除换行符外任意字符. 根据m修饰符, 现在表达式匹配每行的结尾.

"/.at(.)?$/" => The fat cat sat on the mat.
Copy after login

在线测试工具

"/.at(.)?$/gm" => The fat cat sat on the mat.
Copy after login

在线测试工具

6. 贪婪匹配与惰性匹配 (Greedy vs lazy matching)

  正则表达式默认采用贪婪匹配模式,在该模式下意味着会匹配尽可能长的子串。我们可以使用?将贪婪匹配模式转化为惰性匹配模式。

"/(.*at)/" => The fat cat sat on the mat.
Copy after login

在线测试工具

"/(.*?at)/" => The fat cat sat on the mat.
Copy after login

在线测试工具

相关推荐:

  1、PHP正则表达式合集

 2.Regular expression video tutorial

 3.Regular expression is a manual download

 4.Regular expression online testing tool

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!