正则表达式速查表 - Something TO DO

正则表达式速查表

Something TO DO posted @ 2017年12月25日 09:21 in C++ with tags 正则表达式 Qt , 983 阅读
正则表达式速查表
 
正则表达式速查表
正则表达式30分钟入门教程 | 常用正则表达式 | 正则表达式测试工具 | 正则表达式 | 正则练习器在线版
字符 描述
\ 将下一个字符标记为一个特殊字符、或一个原义字符、或一个向后引用、或一个八进制转义符。例如,“n"匹配字符"n"。"\n"匹配一个换行符。串行"\\"匹配"\"而"\("则匹配"("。
^ 匹配输入字符串的开始位置。如果设置了RegExp对象的Multiline属性,^也匹配“\n"或"\r"之后的位置。
$ 匹配输入字符串的结束位置。如果设置了RegExp对象的Multiline属性,$也匹配“\n"或"\r"之前的位置。
* 匹配前面的子表达式零次或多次。例如,zo*能匹配“z"以及"zoo"。*等价于{0,}。
+ 匹配前面的子表达式一次或多次。例如,“zo+"能匹配"zo"以及"zoo",但不能匹配"z"。+等价于{1,}。
? 匹配前面的子表达式零次或一次。例如,“do(es)?"可以匹配"does"或"does"中的"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} mn均为非负整数,其中n<=m。最少匹配n次且最多匹配m次。例如,“o{1,3}"将匹配"fooooood"中的前三个o。"o{0,1}"等价于"o?"。请注意在逗号和两个数之间不能有空格。
? 当该字符紧跟在任何一个其他限制符(*,+,?,{n},{n,},{n,m})后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。例如,对于字符串“oooo","o+?"将匹配单个"o",而"o+"将匹配所有"o"。
. 匹配除“\n"之外的任何单个字符。要匹配包括"\n"在内的任何字符,请使用像"(.|\n)"的模式。
(pattern) 匹配pattern并获取这一匹配。所获取的匹配可以从产生的Matches集合得到,在VBScript中使用SubMatches集合,在JScript中则使用$0…$9属性。要匹配圆括号字符,请使用“\("或"\)"。
(?:pattern) 匹配pattern但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用或字符“(|)"来组合一个模式的各个部分是很有用。例如"industr(?:y|ies)"就是一个比"industry|industries"更简略的表达式。
(?=pattern) 正向肯定预查,在任何匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如,“Windows(?=95|98|NT|2000)"能匹配"Windows2000"中的"Windows",但不能匹配"Windows3.1"中的"Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。
(?!pattern) 正向否定预查,在任何不匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如“Windows(?!95|98|NT|2000)"能匹配"Windows3.1"中的"Windows",但不能匹配"Windows2000"中的"Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始
(?<=pattern) 反向肯定预查,与正向肯定预查类拟,只是方向相反。例如,“(?<=95|98|NT|2000)Windows"能匹配"2000Windows"中的"Windows",但不能匹配"3.1Windows"中的"Windows"。
(?<!pattern) 反向否定预查,与正向否定预查类拟,只是方向相反。例如“(?<!95|98|NT|2000)Windows"能匹配"3.1Windows"中的"Windows",但不能匹配"2000Windows"中的"Windows"。
x|y 匹配x或y。例如,“z|food"能匹配"z"或"food"。"(z|f)ood"则匹配"zood"或"food"。
[xyz] 字符集合。匹配所包含的任意一个字符。例如,“[abc]"可以匹配"plain"中的"a"。
[^xyz] 负值字符集合。匹配未包含的任意字符。例如,“[^abc]"可以匹配"plain"中的"p"。
[a-z] 字符范围。匹配指定范围内的任意字符。例如,“[a-z]"可以匹配"a"到"z"范围内的任意小写字母字符。
[^a-z] 负值字符范围。匹配任何不在指定范围内的任意字符。例如,“[^a-z]"可以匹配任何不在"a"到"z"范围内的任意字符。
\b 匹配一个单词边界,也就是指单词和空格间的位置。例如,“er\b"可以匹配"never"中的"er",但不能匹配"verb"中的"er"。
\B 匹配非单词边界。“er\B"能匹配"verb"中的"er",但不能匹配"never"中的"er"。
\cx 匹配由x指明的控制字符。例如,\cM匹配一个Control-M或回车符。x的值必须为A-Z或a-z之一。否则,将c视为一个原义的“c"字符。
\d 匹配一个数字字符。等价于[0-9]。
\D 匹配一个非数字字符。等价于[^0-9]。
\f 匹配一个换页符。等价于\x0c和\cL。
\n 匹配一个换行符。等价于\x0a和\cJ。
\r 匹配一个回车符。等价于\x0d和\cM。
\s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。
\S 匹配任何非空白字符。等价于[^ \f\n\r\t\v]。
\t 匹配一个制表符。等价于\x09和\cI。
\v 匹配一个垂直制表符。等价于\x0b和\cK。
\w 匹配包括下划线的任何单词字符。等价于“[A-Za-z0-9_]"。
\W 匹配任何非单词字符。等价于“[^A-Za-z0-9_]"。
\xn 匹配n,其中n为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如,“\x41"匹配"A"。"\x041"则等价于"\x04&1"。正则表达式中可以使用ASCII编码。.
\num 匹配num,其中num是一个正整数。对所获取的匹配的引用。例如,“(.)\1"匹配两个连续的相同字符。
\n 标识一个八进制转义值或一个向后引用。如果\n之前至少n个获取的子表达式,则n为向后引用。否则,如果n为八进制数字(0-7),则n为一个八进制转义值。
\nm 标识一个八进制转义值或一个向后引用。如果\nm之前至少有nm个获得子表达式,则nm为向后引用。如果\nm之前至少有n个获取,则n为一个后跟文字m的向后引用。如果前面的条件都不满足,若nm均为八进制数字(0-7),则\nm将匹配八进制转义值nm
\nml 如果n为八进制数字(0-3),且m和l均为八进制数字(0-7),则匹配八进制转义值nml。
\un 匹配n,其中n是一个用四个十六进制数字表示的Unicode字符。例如,\u00A9匹配版权符号(©)。

常用正则表达式
用户名 /^[a-z0-9_-]{3,16}$/
密码 /^[a-z0-9_-]{6,18}$/
密码2 (?=^.{8,}$)(?=.*\d)(?=.*\W+)(?=.*[A-Z])(?=.*[a-z])(?!.*\n).*$ (由数字/大写字母/小写字母/标点符号组成,四种都必有,8位以上)
十六进制值 /^#?([a-f0-9]{6}|[a-f0-9]{3})$/
电子邮箱 /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
/^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/或\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
URL /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ 或 [a-zA-z]+://[^\s]*
IP 地址 /((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)/
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ 或 ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)
HTML 标签 /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/或<(.*)(.*)>.*<\/\1>|<(.*) \/>
删除代码\\注释 (?<!http:|\S)//.*$
匹配双字节字符(包括汉字在内) [^\x00-\xff]
汉字(字符) [\u4e00-\u9fa5]
Unicode编码中的汉字范围 /^[\u2E80-\u9FFF]+$/
中文及全角标点符号(字符) [\u3000-\u301e\ufe10-\ufe19\ufe30-\ufe44\ufe50-\ufe6b\uff01-\uffee]
日期(年-月-日) (\d{4}|\d{2})-((0?([1-9]))|(1[1|2]))-((0?[1-9])|([12]([1-9]))|(3[0|1]))
日期(月/日/年) ((0?[1-9]{1})|(1[1|2]))/(0?[1-9]|([12][1-9])|(3[0|1]))/(\d{4}|\d{2})
时间(小时:分钟, 24小时制) ((1|0?)[0-9]|2[0-3]):([0-5][0-9])
中国大陆固定电话号码 (\d{4}-|\d{3}-)?(\d{8}|\d{7})
中国大陆手机号码 1\d{10}
中国大陆邮政编码 [1-9]\d{5}
中国大陆身份证号(15位或18位) \d{15}(\d\d[0-9xX])?
非负整数(正整数或零) \d+
正整数 [0-9]*[1-9][0-9]*
负整数 -[0-9]*[1-9][0-9]*
整数 -?\d+
小数 (-?\d+)(\.\d+)?
空白行 \n\s*\r 或者 \n\n(editplus) 或者 ^[\s\S ]*\n 
QQ号码 [1-9]\d{4,}
不包含abc的单词 \b((?!abc)\w)+\b
匹配首尾空白字符 ^\s*|\s*$
编辑常用
以下是针对特殊中文的一些替换(editplus)
 
^[0-9].*\n 
 
^[^第].*\n 
 
^[习题].*\n
 
^[\s\S ]*\n 
^[0-9]*\. 
^[\s\S ]*\n 
<p[^<>*]>
href="javascript:ifconfirm\((.?)\)window\.location='(.*?)'"
<span style=".[^"]*rgb255,255,255">.[^<>]*</span>

<DIV class=xs0>[\s\S]*?</DIV>

参考资料:

[1]  http://www.jb51.net/tools/regexsc.htm

Avatar_small
Things to do 说:
2022年5月18日 20:19

Fill up your travel map with the help of Things to do post - seize the opportunity to discover the world around you.

Avatar_small
Osmoses Technology 说:
2022年8月08日 01:06

OSMOSE technology private ltd is a Pune-based organisation that provides multiple services platforms for individuals. Technology services such as gaming, eCommerce, social media platforms, and more similar are provided by OSMOSE technology. In this article we do bring some information about how to log in to the website and do bring some important information about these OSMOSE technology pvt ltd. Osmoses Technology Individuals have to visit the official cPanel page to access the services from this portal. the information technology company, engaged to develop and maintain networking applications, mobile applications, web-based applications, and they also undertake outsourcing services related to information technology to provide extended support to individuals.

Avatar_small
1st Inter Model Pape 说:
2022年8月18日 16:20

Students taking the class 11 exams administered by the Uttarakhand Board may review the important model question paper from 2023 for better preparation. On the official website, you may obtain the 11th Important Model Question Paper for 2023 for the Science, Commerce, and Arts streams. However, the Board will shortly publish the condensed Important Model Question Paper 2023 for the upcoming academic year. 1st Inter Model Paper 2023 Students may verify the marking scheme and the Important Model Question Paper 2023 using this UK Board 11th Practice exam. The 11th Significant Model Question Paper from the Uttarakhand Board for 2023 also lists important themes for each subject, such as English, Economics, Geography, and Mathematics, unit by unit.

Avatar_small
Navasakam3.apcfss.in 说:
2022年10月27日 00:39

The Andhra Pradesh state has once again implemented a new scheme which upholds the welfare of the state citizens. The scheme YSR Navasakam was established to execute other welfare schemes which are beneficial to the citizens. Navasakam3.apcfss.in/login.do The government has also introduced a self-service website portal for citizens to avail of their services from the page. Andhra Pradesh citizens can now acquire state cars and other important information from the site.

Avatar_small
AP Health Card 2023 说:
2022年11月09日 17:31

A medical cover is a huge privilege which is significant to everyone. It’s a guarantee of proper medication for you and your family members. However, paying for the cover or medical card might be challenging since it might cost more than you can offer. To cater to such the Andhra Pradesh state has introduced a Health card scheme for state employees. The scheme is known as Employee Health card scheme. AP Health Card 2023 Login It’s created to help AP state government employees receive medical coverage and benefits during emergencies.

Avatar_small
SBI Net Banking 说:
2023年1月29日 22:31

State Bank of India is one of the largest Indian banking services which is also spread widely across the country and in foreign nations as well. And it is a great decision if you have already opened up your banking account SBI Net Banking with the SBI because you will understand and get more benefits that you can avail of through their online banking service through online and from their Yono app as well to use SBI credit card and debit card services too.

Avatar_small
10thmodelquestionsp 说:
2023年7月01日 21:36

professional writers'collective effort to produce specialised news coverage of the10thmodelquestionspapers.in most recent events in the nation Our team is made up of professional writers and citizen journalists with a wide range of journalism interests who are enthusiastic about disseminating the education updates transparently and in the benefit of the general public.For all age groups,our reporting team plans to publish the Education & Recruitment Update and provide inside coverage


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee