当前位置:网站首页>正则匹配以XXX开头的,XXX结束的
正则匹配以XXX开头的,XXX结束的
2022-07-07 06:30:00 【大锤爱编程】
目录
前言
正则匹配是描述某种规则的表达式,能够很大程度提升字符串摘取和字符串匹配的工作效率。
一、正则匹配是什么?
正则表达式(英语:Regular Expression,常简写为regex、regexp或RE),又称正则表示式、正则表示法、规则表达式、常规表示法是计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串。比如Linux系统中的Grep命令
二、正则匹配语法练习
1、全字符串匹配
全字符串匹配就是素正则表达式,就是一个简单的查找功能,比如查询was,test这些英文单次而已。
正则表达式:was
2、匹配XXX开始、XXX结束、XXX
在正则表达式的语法中有^开头的,这表明以XXX来作为开头,下面的这个例子就是匹配以sh作为开头的单词~
正则表达式:^sh
在正则表达式的语法中有$结束的,这表明以XXX来作为开头,下面的这个例子就是匹配以lls作为结束的单词~
正则表达式:lls$
总结
今天简单介绍了正则匹配当中的以XXX开始和以XXX结尾的语法,后面会找一些重头戏来详细剖析。
最后的最后,放一点儿正则表达式福利:
去掉左右空格:
str.replace(/(^\s*)|(\s*$)/g, '')
去掉所有空格:
str.replace(/\s+/g, '')
密码需由8位以上大写字母、小写字母、数字及特殊符号组成:
/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!.,@$%^&*-]).{8,}$/
Email地址:
^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$
域名:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?
InternetURL:[a-zA-z]+://[
^s]* 或
^http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$`手机号码:
^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])d{8}$
电话号码(“XXX-XXXXXXX”、”XXXX-XXXXXXXX”、”XXX-XXXXXXX”、”XXX-XXXXXXXX”、”XXXXXXX”和”XXXXXXXX):
^((d{3,4}-)|d{3.4}-)?d{7,8}$
国内电话号码
(0511-4405222、021-87888822):d{3}-d{8}|d{4}-d{7}
身份证号(15位、18位数字):
^d{15}|d{18}$
短身份证号码(数字、字母x结尾):
^([0-9]){7,18}(x|X)?$
或^d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$
帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):
^[a-zA-Z][a-zA-Z0-9_]{4,15}$
密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线):
^[a-zA-Z]w{5,17}$
强密码(必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间):
^(?=.*d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
日期格式:
^d{4}-d{1,2}-d{1,2}
一年的12个月(01~09和1~12):
^(0?[1-9]|1[0-2])$
一个月的31天(01~09和1~31):
^((0?[1-9])|((1|2)[0-9])|30|31)$
xml文件:^([a-zA-Z]+-?)+[a-zA-Z0-9]+\.[x|X][m|M][l|L]$
空白行的正则表达式:
s*
(可以用来删除空白行)HTML标记的正则表达式:
<(S*?)[`^>]*>.*?</>|<.*? />
(网上流传的版本太糟糕,上面这个也仅仅能部分,对于复杂的嵌套标记依旧无能为力)首尾空白字符的正则表达式:
^s*|s*$
或(^s*)|(s*$
) (可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式)腾讯QQ号:
[1-9][0-9]{4,}
(腾讯QQ号从10000开始)中国邮政编码:
[1-9]d{5}(?!d)
(中国邮政编码为6位数字)IP地址:
((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))
边栏推荐
- Detailed learning notes of JVM memory structure (I)
- Hard core sharing: a common toolkit for hardware engineers
- Cesium does not support 4490 problem solution and cesium modified source code packaging scheme
- Synchronized underlying principle, volatile keyword analysis
- OpenGL frame buffer
- On December 8th, 2020, the memory of marketing MRC application suddenly increased, resulting in system oom
- DRF authentication, permissions, and flow restrictions (only for views in DRF)
- Simulation volume leetcode [general] 1567 Length of the longest subarray whose product is a positive number
- STM32 clock system
- C language pointer (Part 2)
猜你喜欢
How to pass the PMP Exam in a short time?
外部中断实现按键实验
Cesium does not support 4490 problem solution and cesium modified source code packaging scheme
Detailed learning notes of JVM memory structure (I)
What is the use of PMP certificate?
C语言指针(中篇)
C language pointer (Part 1)
Entity of cesium data visualization (Part 1)
Selenium mouse sliding operation event
串口實驗——簡單數據收發
随机推荐
个人力扣题目分类记录
E-commerce campaign Guide
Locust performance test 3 (high concurrency, parameter correlation, assembly point)
【Istio Network CRD VirtualService、Envoyfilter】
JVM 内存结构 详细学习笔记(一)
Simulation volume leetcode [general] 1557 The minimum number of points that can reach all points
Pytest+request+allure+excel interface automatic construction from 0 to 1 [five nails / flying Book notice]
Interpretation of MySQL optimization principle
Storage of data in memory
Leetcode question brushing record (array) combination sum, combination sum II
Reflections on the way of enterprise IT architecture transformation (Alibaba's China Taiwan strategic thought and architecture practice)
Hard core sharing: a common toolkit for hardware engineers
C language pointer (exercises)
模拟卷Leetcode【普通】1557. 可以到达所有点的最少点数目
硬件大熊原创合集(2022/06更新)
Implementation of corner badge of Youmeng message push
What are the conditions for applying for NPDP?
模拟卷Leetcode【普通】1705. 吃苹果的最大数目
Two schemes of unit test
Postman interface test (I. installation and use)