修为突破灵药-正则表达式

Regular Expressions,缩写为 Regex 或 Regexp

分割线

fix-记录

  • https://github.com/bubkoo/hexo-filter-fenced-code/issues/3

    1
    var rFenceCode = /(\s*)(`{3,}|~{3,}) *(.*) *\n?([\s\S]+?)\s*(\2)(\n+|$)/g;
    regex解释
    (\s*)(`{3,} | ~{3,})``` 或者 ~~~
    *(.*)至少 1 个空格+至少 1 个任意字符
    *\n?至少 1 个空格+可有可无换行
    错误原因就是此,我习惯 trim 行尾的空格,所以无法正常匹配

分割线

regex-多对多替换

  • 我在切换图床时想实现如下 多对多替换 的需求

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    https://fastly.jsdelivr.net/gh/Weidows/Images/post/1d9Xs6ADR3MaNCy.png
    --> https://www.helloimg.com/images/2022/02/27/GVFbWK.jpg

    https://fastly.jsdelivr.net/gh/Weidows/Images/post/2C7cgeEIQNr3qLu.png
    --> https://www.helloimg.com/images/2022/02/27/GVFlgb.jpg

    https://fastly.jsdelivr.net/gh/Weidows/Images/post/2GXOS5mW8EzIT7Y.png
    --> https://www.helloimg.com/images/2022/02/27/GVFWDS.jpg

    https://fastly.jsdelivr.net/gh/Weidows/Images/post/2POdUowc3qW8DRy.png
    --> https://www.helloimg.com/images/2022/02/27/GVFTHD.jpg

    https://fastly.jsdelivr.net/gh/Weidows/Images/post/2ThkbFZmCU3QvEN.png
    --> https://www.helloimg.com/images/2022/02/27/GVF2ao.jpg

    https://fastly.jsdelivr.net/gh/Weidows/Images/post/2ThYuqlEtFfdJeK.png
    --> https://www.helloimg.com/images/2022/02/27/GVFOGC.jpg
  • shell 脚本实现:

    支持同一链接在多个文件/多次出现出现的情况

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    ###
    # @?: *********************************************************************
    # @Author: Weidows
    # @Date: 2022-02-27 01:34:37
    # @LastEditors: Weidows
    # @LastEditTime: 2022-02-27 18:29:00
    # @FilePath: \Blog-private\test.sh
    # @Description:
    # @!: *********************************************************************
    ###

    # fileArray=(
    # 1d9Xs6ADR3MaNCy
    # 2C7cgeEIQNr3qLu
    # 2GXOS5mW8EzIT7Y
    # 2POdUowc3qW8DRy
    # 2ThkbFZmCU3QvEN
    # )
    fileArray=(

    )

    # urlArray=(
    # 2022/02/27/GVFbWK.jpg
    # 2022/02/27/GVFlgb.jpg
    # 2022/02/27/GVFWDS.jpg
    # 2022/02/27/GVFTHD.jpg
    # 2022/02/27/GVF2ao.jpg
    # )
    urlArray=(

    )

    for i in "${!fileArray[@]}"; do
    # 查找
    path=`grep -rl ${fileArray[$i]} ./source`

    # 替换
    sed -i "s#cdn.jsdelivr.net/gh/Weidows/Images/post/${fileArray[$i]}\.\w\w\w#www.helloimg.com/images/${urlArray[$i]}#g" $path
    done

分割线

vscode-批量替换

1
2
3
4
5
Screenshot_20210313_171408_tv.danmaku.bili
-> Screenshot20210313171408

Screenshot_\d+_\d+_tv.danmaku.bili`
-> Screenshot\d+\d+

  • 匹配下面内容

    1
    2
    3
    4
    5
    6
    categories:
    - experience
    - shell
    tags:
    - experience
    - shell
  1. categories:*tags:

    行不通, 其中间含有 \n

  2. categories:\n((.)±(.)+\n)+tags:\n((.)±(.)+\n)+

分割线

sed-替换反斜杠

  • 数据中的斜杠是脚本天敌,需要做预处理转义
    [1]

    1
    2
    2022/02/27/GVJOM6.jpg
    -> 2022\/02\/27\/GVJOM6.jpg

    sed -i "s#/#\\/#g" result.txt

分割线

借物表

[1]: Sed 替换 内容带反斜杠(/)