diff --git a/docs/regex.html b/docs/regex.html index 1fc85f03..ea1f4368 100644 --- a/docs/regex.html +++ b/docs/regex.html @@ -47,6 +47,8 @@
  • MySQL 中的 Regex (Quick Reference)
  • Vim 中的 Regex (Quick Reference)
  • 在线 Regex 测试器 (regex101.com)
  • +
  • 轻松学习 Regex (github.com)
  • +
  • 正则表达式实例搜集 (jaywcjlove.github.io)
  • 字符类

    @@ -87,7 +89,7 @@ -
    范例说明
    [abc]单个字符:a、b 或 c
    [^abc]一个字符,除了:a、b 或 c
    [a-z]范围内的字符:a-z
    [^a-z]不在范围内的字符:a-z
    [0-9]范围内的数字:0-9
    [a-zA-Z]范围内的字符:
    a-z 或 A-Z
    [a-zA-Z0-9]范围内的字符:
    a-z、A-Z 或 0-9
    +
    范例说明
    [abc]单个字符:abc
    [^abc]一个字符,除了:abc
    [a-z]范围内的字符:a-z
    [^a-z]不在范围内的字符:a-z
    [0-9]范围内的数字:0-9
    [a-zA-Z]范围内的字符:
    a-zA-Z
    [a-zA-Z0-9]范围内的字符:
    a-zA-Z0-9

    量词

    @@ -552,7 +554,23 @@ -
    :--
    (?=...)正先行断言
    (?!...)负先行断言
    (?<=...)正后发断言
    (?<!...)负后发断言
    + + + + + + + + + + + + + + + + +
    :--
    (?=...)正先行断言
    (?!...)负先行断言
    (?<=...)正后发断言
    (?<!...)负后发断言
    ?= 正先行断言-存在
    ?! 负先行断言-排除
    ?<=正后发断言-存在
    ?<!负后发断言-排除

    零宽度断言 允许您在主模式之前(向后看)或之后(lookahead)匹配一个组,而不会将其包含在结果中。

    标志/修饰符

    @@ -1199,10 +1217,8 @@
    范例说明
    (?>red|green|blue)比非捕获更快
    (?>id|identity)\b匹配 id,但不匹配 identity

    "id" 匹配,但 \b 在原子组之后失败, -解析器不会回溯到组以重试“身份” -
    -
    -如果替代品重叠,请从长到短命令。

    +解析器不会回溯到组以重试“身份”

    +

    如果替代品重叠,请从长到短命令。

    零宽度断言 Lookaround(前后预查)

    @@ -1255,7 +1271,474 @@

    匹配 Mr.Ms. 如果单词 her 稍后在字符串中

    M(?(?=.*?\bher\b)s|r)\.
     
    -

    需要环顾 IF 条件

    +

    需要环顾 IF 条件

    +

    基础实例

    +

    基本匹配

    + + + + + + + + + + + + + + + + + +
    表达式匹配示例
    theThe fat cat sat on the mat.
    TheThe fat cat sat on the mat.
    + +

    由字母t开始,接着是h,再接着是e

    +

    点运算符 .

    + + + + + + + + + + + + + +
    表达式匹配示例
    .arThe car parked in the garage.
    + +

    表达式.ar匹配一个任意字符后面跟着是ar的字符串

    +

    字符集

    + + + + + + + + + + + + + + + + + +
    表达式匹配示例
    .arThe car parked in the garage.
    ar[.]A garage is a good place to park a car.
    + +

    方括号的句号就表示句号。表达式 ar[.] 匹配 ar. 字符串

    +

    否定字符集

    + + + + + + + + + + + + + +
    表达式匹配示例
    [^c]arThe car parked in the garage.
    + +

    表达式 [^c]ar 匹配一个后面跟着 ar 的除了c的任意字符。

    +

    重复次数

    + +

    *

    + + + + + + + + + + + + + + + + + +
    表达式匹配示例
    [a-z]*The car parked in the garage #21.
    \s*cat\s*The fat cat sat on the concatenation.
    +

    表达式 [a-z]* 匹配一个行中所有以小写字母开头的字符串。

    +

    +

    + + + + + + + + + + + + + +
    表达式匹配示例
    c.+tThe fat cat sat on the mat.
    +

    表达式 c.+t 匹配以首字母c开头以t结尾,中间跟着至少一个字符的字符串。

    +

    ?

    + + + + + + + + + + + + + + + + + +
    表达式匹配示例
    [T]heThe car is parked in the garage.
    [T]?heThe car is parked in the garage.
    +

    表达式 [T]?he 匹配字符串 heThe

    +

    {}

    + + + + + + + + + + + + + + + + + + + + + +
    表达式匹配示例
    [0-9]{2,3}The number was 9.9997 but we rounded it off to 10.0.
    [0-9]{2,}The number was 9.9997 but we rounded it off to 10.0.
    [0-9]{3}The number was 9.9997 but we rounded it off to 10.0.
    + +

    (...) 特征标群

    + + + + + + + + + + + + + +
    表达式匹配示例
    (c|g|p)arThe car is parked in the garage.
    + +

    表达式 (c|g|p)ar 匹配 cargarpar。 注意 \ 是在 Markdown 中为了不破坏表格转义 |

    +

    | 或运算符

    + + + + + + + + + + + + + +
    表达式匹配示例
    (T|t)he|carThe car is parked in the garage.
    + +

    表达式 (T|t)he|car 匹配 (T|t)hecar

    +

    转码特殊字符

    + + + + + + + + + + + + + +
    表达式匹配示例
    (f|c|m)at\.?The fat cat sat on the mat.
    + +

    如果想要匹配句子中的 . 则要写成 \. 以下这个例子 \.? 是选择性匹配.

    +

    锚点

    +

    ^

    + + + + + + + + + + + + + + + + + +
    表达式匹配示例
    (T|t)heThe car is parked in the garage.
    ^(T|t)heThe car is parked in the garage.
    + +

    $

    + + + + + + + + + + + + + + + + + +
    表达式匹配示例
    (at\.)The fat cat. sat. on the mat.
    (at\.)$The fat cat. sat. on the mat.
    + +

    简写字符集

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    简写描述
    .除换行符外的所有字符
    \w匹配所有字母数字,等同于 [a-zA-Z0-9_]
    \W匹配所有非字母数字,即符号,等同于: [^\w]
    \d匹配数字: [0-9]
    \D匹配非数字: [^\d]
    \s匹配所有空格字符,等同于: [\t\n\f\r\p{Z}]
    \S匹配所有非空格字符: [^\s]
    \f匹配一个换页符
    \n匹配一个换行符
    \r匹配一个回车符
    \t匹配一个制表符
    \v匹配一个垂直制表符
    \p匹配 CR/LF(等同于 \r\n),用来匹配 DOS 行终止符
    + +

    正则表达式提供一些常用的字符集简写。

    +

    ?=... 正先行断言

    + + + + + + + + + + + + + +
    表达式匹配示例
    (T|t)he(?=\sfat)The fat cat sat on the mat.
    + +

    Thethe 后面紧跟着 (空格)fat

    +

    ?!... 负先行断言

    + + + + + + + + + + + + + +
    表达式匹配示例
    (T|t)he(?!\sfat)The fat cat sat on the mat.
    + +

    匹配 Thethe,且其后不跟着 (空格)fat

    +

    ?<= ... 正后发断言

    + + + + + + + + + + + + + +
    表达式匹配示例
    (?<=(T|t)he\s)(fat|mat)The fat cat sat on the mat.
    + +

    匹配 fatmat,且其前跟着 Thethe

    +

    ?<!... 负后发断言

    + + + + + + + + + + + + + +
    表达式匹配示例
    (?<!(T|t)he\s)(cat)The cat sat on cat.
    + +

    匹配 cat,且其前不跟着 Thethe

    +

    忽略大小写 (Case Insensitive)

    + + + + + + + + + + + + + + + + + +
    表达式匹配示例
    TheThe fat cat sat on the mat.
    /The/giThe fat cat sat on the mat.
    + +

    修饰语 i 用于忽略大小写,g 表示全局搜索。

    +
    + + + + + + + + + + + + + + + + + +
    表达式匹配示例
    /.(at)/The fat cat sat on the mat.
    /.(at)/gThe fat cat sat on the mat.
    + +

    表达式 /.(at)/g 表示搜索 任意字符(除了换行)+ at,并返回全部结果。

    +

    多行修饰符 (Multiline)

    + + + + + + + + + + + + + + + + + +
    表达式匹配示例
    /.at(.)?$/The fat
    cat sat
    on the mat.
    /.at(.)?$/gmThe fat
    cat sat
    on the mat.
    + +

    贪婪匹配与惰性匹配 (Greedy vs lazy matching)

    + + + + + + + + + + + + + + + + + +
    表达式匹配示例
    /(.*at)/The fat cat sat on the mat.
    /(.*?at)/The fat cat sat on the mat.
    +

    Python 中的正则表达式

    入门

    导入正则表达式模块

    @@ -1725,5 +2208,11 @@ mysql> SELECT regexp_instr('abbabba', 'b{2}', 1, 3, 1); 7 -
    +

    也可以看看

    + +