当前位置:网站首页>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
参考资料:
边栏推荐
- sql concat() function
- Flink-流/批/OLAP一体得到Flink引擎
- 【MySQL功法】第5话 · SQL单表查询
- protobuf编码及网络通信应用(一)
- Misc of CTF-Memory Analysis (Volatility)
- 使用PyQt5为YoloV5添加界面(一)
- Servlet basic principles and application of common API methods
- The Request request body is repackaged to solve the problem that the request body can only be obtained once
- misc-file steganography of CTF
- Function 函数式接口及应用
猜你喜欢

MySQL 数据类型及占用空间
![[Mozhe Academy] Identity Authentication Failure Vulnerability Actual Combat](/img/c3/4a4e23a97e4650a17ff5cfc5233043.png)
[Mozhe Academy] Identity Authentication Failure Vulnerability Actual Combat

FastAPI 快速入门

网上说的挖矿究竟是什么? 挖矿系统开发详解介绍

mysql不是内部或外部命令,也不是可运行的程序或批处理文件解决

Online sql editing query tool sql-editor

Mycat2.0搭建教程

mysql is not an internal or external command, nor is it a runnable program or batch file to resolve

JVM学习(二) 垃圾收集器
![CTFSHOW command execution [web29-web124] unfinished to be continued](/img/89/786fbe65af4c9f269530bf2d08e1a0.png)
CTFSHOW command execution [web29-web124] unfinished to be continued
随机推荐
Extraction of BaseDAO
Volatility memory forensics - command shows
CTFSHOW command execution [web29-web124] unfinished to be continued
C# WPF下限制TextBox只输入数字、小数点、删除等键
kali is an essential artifact for information security
JVM Learning (2) Garbage Collector
2022CISCNmisc
Deserialization character escape
TDengineGUI无法连接TDengine
使用PyQt5为YoloV5添加界面(一)
在线sql编辑查询工具sql-editor
sql concat()函数
学生成绩管理系统(C语言版)
Trust anchor for certification path not found.异常解决方法。
Awd summary
Monstache执行monstache -f config.toml出错No processor type exists with name [attachment] [type=parse_exc
C#中使用OleDb操作access数据库
nodejs PM2监控及报警邮件发送(二)
Flink PostgreSQL CDC配置和常见问题
Student achievement management system (C language version)