美文网首页
10 分钟正则旅程

10 分钟正则旅程

作者: zvving | 来源:发表于2017-04-28 17:38 被阅读50次

如果说技术人员学习什么知识点『性价比』更高,正则表达式算是一个不错的选择。
不去开始学习一个东西很多时候只是因为我们还不了解它,多少次需要用到的时候粗略 Google 了事,失去一次学习的机会。
其实略有基础的工程师都能在一个小时内快速掌握正则表达式的基本用法,后续偶尔复习两次,也就会了。

不信,顺着下面的教程,我们花 10 分钟上手试试。

Patterns 截图

这里推荐建议把下面的内容复制到 Patterns 『Search Text』中,然后按住教程内容输入 -> 后面的正则表达式,揣摩高亮的匹配结果。

入门( 5 min )

// Q: 匹配个 world 试试
hello, world!                   -> world

// 括号是什么?
// [set] 集合: [] 中的 ^ 表示取非, 如:[^a]
// (group) 分组:多用在替换的时候
// {number min& max} 重复次数
// Q: 猜猜它们的匹配结果分别是什么?
hello, world!                   -> hel, (hel), [hel], [a-z]
hello, a aa aaa aaaa        -> a{2}, a{1,3}
helll hel helhel helhelhel  -> hel{1,3}, (hel){1,3}, [hel]{1,3}, [a-z]{1,3}

// 集合对应的元字符
.       [^\n] 除换行外的所有字符
\d      [^0-9]
\D      [0-9]
\w      [0-9a-zA-Z]
\W      [^0-9a-zA-Z]
\s      [ \t\n]
\S      [^ \t\n]
\b      单词分界
\B      非单词分界

// 拆解单词 
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum some_balabala.
// Q: 试试下面的正则
-> [a-z]{1,}, \w{1,4} , \w\b, \b\w{1,3}\b

// 重复次数元字符
?           {0,1}
*           {0,}
+           {1,}
ac abc abbc abbbc abbbbc    -> abc, ab?c, ab*c, ab+c

// tips:记不住元字符没关系,先把 []{} 用起来,多翻几次文档用多了也就记住了

// ^ 匹配起始
// $ 匹配结束
hello and hello
// Q: 试试下面的正则
-> ^hello, hello$

// | 或
.com
.cn
.org
// Q: 试试下面的正则
-> .(com|cn)$

// 0-255
// Q: 匹配 0-255 之间的数字,下面几行是测试数据
0
1
123
255
256
01
2550
// 一种解法:
-> ^([1]?[1-9]?[0-9]|[2]([1-4][0-9]|5[0-5]))$

5 分钟完成练习了吗?跟得上的话,再来进阶看看。

进阶( 5 min )

Lorem ipsum dolor sit amet, 
consectetur adipisicing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
Ut enim ad minim veniam, 
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

// () 括号用来捕获 (exp)
// 捕获并命名 (?<name>exp)
// Q: 让所有的单词重复
-> \w*              [replace with] $0$0

// Q: 把匹配括号中捕获的结果第一个和第二个对调
-> (dol)(or)            [replace with] $2$1
// 有命名捕获的写法
-> (?<nameone>dol)(?<nametwo>or)
        [replace with] $nametwo$nameone

// (?:exp) 不捕获, 看看 $0, $1, $2, $3 分别是什么
-> do(l)(?:o)(r)

// foo(?=exp) 匹配 exp 前面的 foo
// Q: 替换使以 t 结尾的单词 t 之前的内容重复
-> \w+(?=t\b)           [replace with] $0$0

// (?<=exp)foo 匹配 exp 后面的 foo
// Q: 匹配单词中 i 后面的部分(不包含 i)
-> (?<=i)\w+

// foo(?!exp) 匹配后面跟的不是 exp 的 foo
// Q: 匹配后面不是 m 或 n 的所有 i
-> i(?![mn])

// (?<!exp)foo 匹配前面不是exp 的 foo
// Q: 匹配前面不是 i 的所有 m 或 n
-> (?<!i)[mn]

恭喜你完成 『10 分钟』正则旅程。相信掌握上面的内容,基本的正则可以手写,常见的正则也能看懂。
如果想进一步的了解,可以参考如下链接。

参考

工具

相关文章

  • 10 分钟正则旅程

    如果说技术人员学习什么知识点『性价比』更高,正则表达式算是一个不错的选择。不去开始学习一个东西很多时候只是因为我们...

  • 十分钟学习正则表达式|上手python re模块

    请给我10分钟,我有把握让你10分钟上手正则表达式。 用一句通俗的语言解释:正则表达式就是记录文本规则的代码。 正...

  • 正则表达式

    正则表达式视频正则表达式30分钟入门教程常用正则表达式大全

  • 30分钟掌握正则表达式

    title: 30分钟掌握正则表达式 正则表达 几个正则表达式编辑器 Debuggex :https://www...

  • 正则表达式

    javascript正则表达式正则表达式30分钟入门教程js的正则表达式的正则前瞻(?=)和非捕获性分组(?:)有...

  • 正则表达式学习链接

    正则表达式学习链接OC 正则表达式30分钟入门教程

  • 23松诺自由书写2021-03-11

    想一段,你想探索的经验,若把这段经验比喻成一段旅程,凝视这段旅程,先进行10-20分钟的自由书写。 看不清前路的旅...

  • iOS正则表达式探索

    参考:正则表达式、正则表达式30分钟入门教程、正则表达式的使用方法 一、概述 正则表达式(Regular Expr...

  • 人人都看得懂的正则表达式教程

    人人都看得懂的正则表达式教程正则简单总结 正则表达式30分钟入门 RegExr http://regex.zj...

  • 正则

    正则表达式30分钟入门教程 iOS Objective-C 正则表达式指南

网友评论

      本文标题:10 分钟正则旅程

      本文链接:https://www.haomeiwen.com/subject/omrazttx.html