当前位置:网站首页>01. 正则表达式概述
01. 正则表达式概述
2022-06-30 06:01:00 【江帅帅】
1. 正则表达式概述
正则表达式,其实是一个「规则表达式」,我们通过设定的规则,可以从某个字符串中匹配出想要的数据。
在 Wikipedia 上,我们看到「正则表达式」的说明如下:
正则表达式(英语:Regular Expression,常简写为regex、regexp或RE),又称正则表示式、正则表示法、规则表达式、常规表示法,是计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串。在很多文本编辑器里,正则表达式通常被用来检索、替换那些匹配某个模式的文本。
许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在 Perl 中就内建了一个功能强大的正则表达式引擎。正则表达式这个概念最初是由 Unix 中的工具软件(例如 sed 和 grep)普及开的。
而更简单去理解的话,其实就是「拿个模子,去套出你想要的数据」。
另外,正则表达式是编程中不可或缺的技术,尤其是现在大家喜欢用 Python 来实现网络爬虫程序,更是能体会到它的强大和魅力。
2. 常见案例(手机号、邮箱校验)
好比,你登录某网站时,账户可以是邮箱,也可以是手机号。此时,系统总会先去校验你输入的邮箱或手机号的格式是否正确,再去判断系统中是否有这个用户。


如果你输入的格式不对,系统就会进行提示,那它是怎么来匹配并验证你输入的信息呢?
比如,手机号的格式匹配,包含了中国电信、中国联通、中国移动、中国广电、上网卡、虚拟运营商等等号段,我们的正则表达式可以这样写:
/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
还有,邮箱的格式匹配,就简单些:
/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
现在看不懂,也没有关系,再复杂的内容,我们后面拆开一个个详细给大家介绍清楚,马上就能看懂了,非常简单的。
边栏推荐
- Mysql database learning notes - foreign keys, table connections, subqueries, and indexes for MySQL multi table queries
- Common mistakes daily practice 01
- [deep learning] data segmentation
- Did you know that WPS can turn on eye protection mode?
- CompletionService使用及原理(源码分析)
- Beauty of Refactoring: when multithreaded batch processing task lifts the beam - Universal scaffold
- [regular expression series] greedy and non greedy patterns
- Zibll子比主题V6.4.1wordpress 开心版源码下载_破解原版/直接使用/无需教程
- 反编译正常回编译出现问题自己解决办法
- 文件操作IO-Part1
猜你喜欢

MySQL高级SQL语句

Do you know how to show the health code in only 2 steps

Talking about the struct of go

Create priority queue

Dynamic programming -- gliding wing of the strange thief Kidd

CompletableFuture从了解到精通,你想知道的这里都有
一个完整的性能测试流程

MySQL 索引

Dao -- a beautiful new world?

Xctf attack and defense world crypto advanced area
随机推荐
Create priority queue
【学习强化学习】总目录
MySQL日志管理、数据备份、恢复
24、 I / O device model (serial port / keyboard / disk / printer / bus / interrupt controller /dma and GPU)
Inno setup the simplest user-defined interface effect
10-【istio】istio Sidecar
Create uiactionsheet [duplicate] - creating uiactionsheet [duplicate]
Dynamic programming -- gliding wing of the strange thief Kidd
Related applications of priority queue
880. 索引处的解码字符串
What indicators should safety service engineers pay attention to in emergency response?
Mariadb数据库的安装与初始化
MySQL事物
Several commands not commonly used in MySQL
How to automatically renew a token after it expires?
Summation of basic exercise sequence of test questions
观察者模式、状态模式在实际工作中的使用
Develop stylelint rules from zero (plug-ins)
飞升:基于中文分词器IK-2种自定义热词分词器构建方式showcase & 排坑showtime
多线程进阶篇