正则表达式

今天看到了正则表达式,就再来重新巩固一遍啦~~~

因为一些东西我都知道啦,所以就写了一个代码,将所发生的情况都试一试,还是直接上手知识来的更快呀

一些功能必要说明都在注释里

测试代码

 1 import re
 2 
 3 text = 'Hi, I am Shirley Hilton. I am his wife.'
 4 text2 = 'site sea sue sweet see case sse ssee loses'
 5 text3 = '312347829412231877187874328709'
 6 text4 = 'aj科 技eu_^%#@^(*&^^-=wio238 jdhfaD按数据库we97r389'
 7 text5 = '(021)88776543 010-55667890 02584453362 0571 66345673 32473 23545346346547900'
 8 
 9 print(re.findall(r"hi", text))   # 是表示单词的开头或者结尾,空格、标点、换行都是单词的分隔。
10 print(re.findall("i.", text))      # .代表匹配任意一个字符
11 print(re.findall(".", text))
12 print(re.findall(r"S", text))     # 不是空白符的任意一个字符
13 print(re.findall(r"iS", text))
14 print(re.findall("I.*e", text))     # *代表前面前面的字符可以重复任意多次(包括0)仅代表数量
15 print(re.findall("I.*?e", text))    # ?懒惰匹配,与*不同,?匹配到最短的就停止
16 print(re.findall(r"sS*?e", text2))    # 如果用 r"s.*?e"  会混进来一个带空格的两个单词,就不是要查询的一个单词了。
17 print(re.findall(r"s.*?e", text2))
18 print(re.findall("1[0-9]{10}", text3))
19 print(re.findall(r"c.*l", text2))
20 print(re.findall(r"d+", text3))
21 print(re.findall("[0-9]*", text3))
22 print(re.findall("[0-9]+", text3))  # +与* 的区别就在于 +是会至少匹配1个, *可以是0
23 print(re.findall(r"w", text4))   # 匹配字母、数字、下划线、汉字
24 print(re.findall(r"s", text4))    # 匹配任意的空白符
25 print(re.findall(r"(0d{2,3})d{7,8}|0d{2,3}[ -]?d{7,8}|d+", text5))
26 #  首先看第一个|之前的 (0d{2,3})是匹配(021) d{7,8}是后面的88776543
27 #  再看下一个0d{2,3}[ -]?d{7,8} 匹配0开头的0xx或者0xxx  然后后面紧跟着空格或者- 这两个符号可以匹配(可有可无?)然后再匹配剩下的后面的数字d{7,8}
28 #  最后面的d+ 就是一个收尾的,前面的匹配不成功的就在这里等着了, 根据管道符从左到右依次匹配,所以d{7,8}放最后啦

执行结果

['hi']
['i,', 'ir', 'il', 'is', 'if']
['H', 'i', ',', ' ', 'I', ' ', 'a', 'm', ' ', 'S', 'h', 'i', 'r', 'l', 'e', 'y', ' ', 'H', 'i', 'l', 't', 'o', 'n', '.', ' ', 'I', ' ', 'a', 'm', ' ', 'h', 'i', 's', ' ', 'w', 'i', 'f', 'e', '.']
['H', 'i', ',', 'I', 'a', 'm', 'S', 'h', 'i', 'r', 'l', 'e', 'y', 'H', 'i', 'l', 't', 'o', 'n', '.', 'I', 'a', 'm', 'h', 'i', 's', 'w', 'i', 'f', 'e', '.']
['i,', 'ir', 'il', 'is', 'if']
['I am Shirley Hilton. I am his wife']
['I am Shirle', 'I am his wife']
['site', 'sue', 'see', 'sse', 'ssee']
['site', 'sea sue', 'sweet see', 'sse', 'ssee']
['12347829412', '18771878743']
['case sse ssee l']
['312347829412231877187874328709']
['312347829412231877187874328709', '']
['312347829412231877187874328709']
['a', 'j', '', '', 'e', 'u', '_', 'w', 'i', 'o', '2', '3', '8', 'j', 'd', 'h', 'f', 'a', 'D', '', '', '', '', 'w', 'e', '9', '7', 'r', '3', '8', '9']
[' ', ' ']
['(021)88776543', '010-55667890', '02584453362', '0571 66345673', '32473', '23545346346547900']

其他一些说明

^ - 匹配字符串的开始

$ - 匹配字符串的结束

S其实就是s的反义,任意不是空白符的字符。同理,还有:

W - 匹配任意不是字母,数字,下划线,汉字的字符

D - 匹配任意非数字的字符

B - 匹配不是单词开头或结束的位置

 ? - 重复零次或一次

{n,} - 重复n次或更多次

{n,m} - 重复n到m次

[a]的反义是[^a],表示除a以外的任意字符。[^abcd]就是除abcd以外的任意字符。

接下来的内容是通关课程中的没见过的知识点 https://www.w3cschool.cn/codecamp/list?pename=regular_expressions_camp

正向和负向Lookahead

Lookahead是一种模式,可以让JavaScript在字符串中预先检查,以便进一步检查模式。当你想在同一个字符串中搜索多个模式时,这可能很有用。

lookaheads有两种:positive lookahead正向预查和negative lookahead负向预查。

positive lookahead将预查确保搜索模式中的元素在那里,但实际上不会匹配它。正向预查是使用(?=...),其中...是不匹配的必需部分。

另一方面,negative lookahead将会预查搜索模式中的元素不存在。负向预查是使用(?!...),其中...是你不想在那里出现的模式。如果不存在负向预查的部分,则返回该模式的其余部分。

1 var quit = "qu";
2 var noquit = "qt";
3 var quRegex= /q(?=u)/;
4 var qRegex = /q(?!u)/;
5 quit.match(quRegex); // 返回 ["q"]
6 noquit.match(qRegex); // 返回 ["q"]

lookaheads的更实际用途是预查一个字符串中的两个或多个模式。以下是一个简单的密码检查器,预查3到6个字符和至少一个数字:

1 var password = "abc123";
2 var checkPass = /(?=w{3,6})(?=D*d)/;
3 checkPass.test(password); // 返回 true

pwRegex中使用lookaheads来匹配长度大于5个字符并具有两个连续数字的密码。

var pwRegex = /(?=[a-z]{3,})(?=D*d{2,})/;

使用捕获组复用模式

你把会重复的模式的正则表达式放在括号之间。

要指定重复字符串的出现位置,你可以使用反斜杠(),然后使用数字。该数字从1开始,并随着你使用的每个附加捕获组而增加。一个例子是1来匹配第一个组。

2就是匹配第二个括号里的捕获组

1 var repeatStr = "regex regex";
2 var repeatRegex = /(w+)s1/;
3 repeatRegex.test(repeatStr); // 返回 true
4 repeatStr.match(repeatRegex); // 返回 ["regex regex", "regex"]

reRexx中使用capture groups捕获组来匹配字符串中出现三次的数字,每个数字由空格分隔

1 var reRegex = /(d+)s(d+)1s2/;
2 var reRegex = /(d+)(d+)s12s/;

以上两种方法,这里真的试了很多次

一共有两个(d+),两个括号两个匹配模式,总之就是要匹配数字,然后是空格符s,然后又是数字,这个我好晕,这两个结果还都是正确的。

使用捕获组进行搜索和替换

1 "Code Camp".replace(/(w+)s(w+)/, '$2 $1');    
2 //返回"Camp Code"

从开头和结尾删除空格

1  var hello = "   Hello, World!  ";
2  var wsRegex = /w.*?!/ig;
3  var result = hello.match(wsRegex);  //得到Hello, World

参考链接
https://www.jianshu.com/p/756367c6c0eb

原文地址:https://www.cnblogs.com/dummersoul/p/12176627.html