[NLP] 2.1 RE 正则表达式

本文整理自 TUM 的英文 NLP 课程。

正则表达式:使用代数符号(例如:+-*/)去表征,查找一系列的字符串,是一种非常简单的分类器(搜索器)。
文中收录的可能不全,如果想要知道更加完整的指南,可以参考:
-快速完整入门指南 (https://deerchao.cn/tutorials/regex/regex.htm)
不必全部阅读完,了解梗概,即可快速上手。


符号 [ ]:里面的内容代表可选的字符

Pattern Matches
[wW]oodchuck Woodchuck or woodchuck
[1234567890] a single digit

符号 [ ‐ ]: 代表范围

Pattern Matches
[A-Z] 大写字符
[a-z] 小写字符
[0-9] 单一数字

符号 [^ ]: 排除某某(如果把^放在第一位)

Pattern Matches Example
[^A-Z] 大写字符 Oyfn pripetchik
[^Ss] 既不是S,也不是s I have no exquisite reason
[e^] e 或者 ^ ****ee Look here
[e] 既不是e,也不是^ ^^ee Look here
a^b 就是 a^b嘛 Look up **a^b **now

符号 | :选择

Pattern Matches
yours|mine yours或者mine
a|b|c 和 [abc] 一样
[gG]roundbog|[Ww]oodchuck 你懂的
grup(y|ies) gruppy 或 gruppies

符号 ? * + {} :表示计数

Pattern Matches exmaple
colou?r 上一个字符可选 color 或 colour
o*h! 0次 或者 多次匹配 h! 或 oh! 或 oooh!
o+h! 1次 或者 多次匹配
a{3,5} 3到5次的a aaa 或 aaaa...
beg.n 匹配任意字符直到 begin 或 begun 或 beg3n

符号 ^ $  B :表示锚定

Pattern Matches Example
^[A-Z] 每行开始处 Palo Alto
^[^A-Za-z] None 1 Hello”
.$ at 每行结尾处 The end.
.$ None The end The end!
the 匹配单词的边界 the world but not other
原文地址:https://www.cnblogs.com/kykai/p/13943347.html