当前位置:网站首页>Detailed explanation of regular expression syntax and practical examples
Detailed explanation of regular expression syntax and practical examples
2022-07-30 06:55:00 【If I don't see you tomorrow】
正则表达式(Regular Expression,常简写为regex、regexp或RE),又称正则表示式、正则表示法、规则表达式、常规表示法,是计算机科学的一个概念.正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串.在很多文本编辑器里,正则表达式通常被用来检索、替换那些匹配某个模式的文本.
expression character
| 表达式 | 描述 |
|---|---|
| \d | any numeric character,等价于[0-9] |
| \D | Any non-numeric character |
| \w | 匹配字母、数字、下划线.等价于[A-Za-z0-9_] |
| \W | 匹配非字母、数字、下划线 |
| \s | 匹配所有空白符,包括换行 |
| \S | 匹配非空白符 |
特殊字符
| 特殊字符 | 描述 |
|---|---|
| . | 匹配除换行符(\n、\r)之外的任何单个字符,相等于 [^\n\r] |
| () | 标记一个子表达式的开始和结束位置.子表达式可以获取供以后使用 |
| [] | 标记一个中括号表达式 |
| {} | 标记限定符表达式 |
| ^ | 匹配输入字符串的开始位置,除非在方括号表达式中使用,当该符号在方括号表达式中使用时,表示不接受该方括号表达式中的字符集合. |
| $ | 匹配输入字符串的结尾位置 |
| | | 逻辑或 |
| \ | 将下一个字符标记为或特殊字符、或原义字符、或向后引用、或八进制转义符 |
限定符
限定符用来指定正则表达式的一个给定组件必须要出现多少次才能满足匹配.有*或+或?或{n}或{n,}或{n,m}共6种.
| 字符 | 描述 |
|---|---|
| * | 匹配前面的子表达式零次或多次.例如,zo* 能匹配 “z” 以及 “zoo”.* 等价于{0,} |
| + | 匹配前面的子表达式一次或多次.例如,‘zo+’ 能匹配 “zo” 以及 “zoo”,但不能匹配 “z”.+ 等价于 {1,} |
| ? | 匹配前面的子表达式零次或一次.例如,“do(es)?” 可以匹配 “do” 、 “does” 中的 “does” 、 “doxy” 中的 “do” .? 等价于 {0,1} |
| {n} | n 是一个非负整数.匹配确定的 n 次.例如,‘o{2}’ 不能匹配 “Bob” 中的 ‘o’,但是能匹配 “food” 中的两个 o |
| {n,} | n 是一个非负整数.至少匹配n 次.例如,‘o{2,}’ 不能匹配 “Bob” 中的 ‘o’,但能匹配 “foooood” 中的所有 o.‘o{1,}’ 等价于 ‘o+’.‘o{0,}’ 则等价于 ‘o*’ |
| {n,m} | m 和 n 均为非负整数,其中n <= m.最少匹配 n 次且最多匹配 m 次.例如,“o{1,3}” 将匹配 “fooooood” 中的前三个 o.‘o{0,1}’ 等价于 ‘o?’.请注意在逗号和两个数之间不能有空格 |
基础实例
[^abc]:不包括a/b/c[A-Z]:匹配大写字母A-Zz{1,3}:匹配1~3个字母z^...$:Matches an expression that starts and ends(a(bc)):捕获子表达式(abc|def):匹配abc或def的表达式
实用实例
recommended toRegexOneLearn about learning regular examples
特定文件

正则表达式:(\w+)\.(jpg|png|gif)$
url解析

正则表达式:(\w+)://([\w\-\.]+)(:(\d+))?
base64
正则表达式:^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$
URL_SAFE模式:安全的URL编码,base64generated during the transcoding process
+、/、=这些会被URL进行转码的特殊字符,替换为-、_、
org.apache.commons.codec.binary.Base64#isBase64(java.lang.String) 识别
A-Za-z0-9+/-_=、、\n、\r、\tis a valid character
参考资料:
边栏推荐
- Function 函数式接口及应用
- Student achievement management system (C language version)
- SQL Server database generation and execution of SQL scripts
- MySQL - Function and Constraint Commands
- 根据ip地址获取地理位置及坐标(离线方式)
- JVM Learning (2) Garbage Collector
- sql中 exists的用法
- Offensive and defensive world easy_web
- oracle row to column, column to row summary
- 【零基础搞定C语言——导航汇总篇】
猜你喜欢
phpok website vulnerability exploitation analysis

标准输入输出流(System.in,System.out)

SSTI range

MySQL - Multi-table query and case detailed explanation

Jdbc & Mysql timeout分析

Monstache执行Monstache - f配置。toml出错不存在处理器类型和名称(附件)(= parse_exc类型

C#预定义数据类型简介

The first WebAssembly program

SQL Server Installation Tutorial

Connect to Mysql in the cloud server Docker detailed graphic and text operations (full)
随机推荐
Usage of exists in sql
php vulnerability full solution
Obtain geographic location and coordinates according to ip address (offline method)
Bypassing the file upload vulnerability
Redis 发布/订阅
ClickHouse查询语句详解
【十年网络安全工程师整理】—100渗透测试工具使用方法介绍
Bubble sort, selection sort, insertion sort, quick sort
GraphQL(一)基础介绍及应用示例
mysql不是内部或外部命令,也不是可运行的程序或批处理文件解决
学生管理系统
DVWA installation tutorial (understand what you don't understand · in detail)
SQL Server database generation and execution of SQL scripts
Jackson serialization failure problem - oracle data return type can't find the corresponding Serializer
Trust anchor for certification path not found.异常解决方法。
[Net Ding Cup 2020 Qinglong Group] AreUSerialz
Using custom annotations, statistical method execution time
[Mozhe Academy] Identity Authentication Failure Vulnerability Actual Combat
【面经】米哈游数据开发面经
vulnhub-XXE ctf security question