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

Regular Expressions,缩写为 Regex 或 Regexp

分割线

fix-记录

regex-matter

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

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

word-matching

https://github.com/imdong/Var-Conv/pull/3

[
"RedirectURL",
"Redirect_URL",
"redirect_url",
"Redirect_url",
"redirect_Url",
"Redirect-url",
"Redirect2-url2",
"REDIRECT_URL",
"REDIRECT2_URL",
"URLParse",
"URL2Parse",
"ParseURL",
].forEach(function (key) {
console.log(
// 1.匹配 redirect / Redirect 这种
// 2.正前瞻 URLPa 这种大写+小写前面的内容 (Pa前面的, 即URL)
// 3.匹配连续大写 REDIRECT 这种
key.match(/([A-Z]?[a-z]+\d*)|([A-Z]+(?=[A-Z][a-z])\d*|[A-Z]+\d*)/g),
key.match(/(^[A-Z]|^|[A-Z])([a-z]+)?/g)
);
});

// [ 'Redirect', 'URL' ] [ 'Redirect', 'U', 'R', 'L' ]
// [ 'Redirect', 'URL' ] [ 'Redirect', 'U', 'R', 'L' ]
// [ 'redirect', 'url' ] [ 'redirect' ]
// [ 'Redirect', 'url' ] [ 'Redirect' ]
// [ 'redirect', 'Url' ] [ 'redirect', 'Url' ]
// [ 'Redirect', 'url' ] [ 'Redirect' ]
// [ 'Redirect2', 'url2' ] [ 'Redirect' ]
// [ 'REDIRECT', 'URL' ] [
// 'R', 'E', 'D', 'I',
// 'R', 'E', 'C', 'T',
// 'U', 'R', 'L'
// ]
// [ 'REDIRECT2', 'URL' ] [
// 'R', 'E', 'D', 'I',
// 'R', 'E', 'C', 'T',
// 'U', 'R', 'L'
// ]
// [ 'URL', 'Parse' ] [ 'U', 'R', 'L', 'Parse' ]
// [ 'URL2', 'Parse' ] [ 'U', 'R', 'L', 'Parse' ]
// [ 'Parse', 'URL' ] [ 'Parse', 'U', 'R', 'L' ]

分割线

regex-多对多替换

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

    https://fastly.jsdelivr.net/gh/Weidows/Images/post/1d9Xs6ADR3MaNCy.png
    --> https://pan.weidows.tech/d/local/blog/GVFbWK.jpg

    https://fastly.jsdelivr.net/gh/Weidows/Images/post/2C7cgeEIQNr3qLu.png
    --> https://pan.weidows.tech/d/local/blog/GVFlgb.jpg

    https://fastly.jsdelivr.net/gh/Weidows/Images/post/2GXOS5mW8EzIT7Y.png
    --> https://pan.weidows.tech/d/local/blog/GVFWDS.jpg

    https://fastly.jsdelivr.net/gh/Weidows/Images/post/2POdUowc3qW8DRy.png
    --> https://pan.weidows.tech/d/local/blog/GVFTHD.jpg

    https://fastly.jsdelivr.net/gh/Weidows/Images/post/2ThkbFZmCU3QvEN.png
    --> https://pan.weidows.tech/d/local/blog/GVF2ao.jpg

    https://fastly.jsdelivr.net/gh/Weidows/Images/post/2ThYuqlEtFfdJeK.png
    --> https://pan.weidows.tech/d/local/blog/GVFOGC.jpg
  • shell 脚本实现:

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

    ###
    # @?: *********************************************************************
    # @Author: Weidows
    # @Date: 2022-02-27 01:34:37
    # @LastEditors: Weidows
    # @LastEditTime: 2022-02-27 18:29:00
    # @FilePath: \Blog-private\test.sh
    # @Description:
    # @!: *********************************************************************
    ###

    echo "hello! now working in:" && pwd

    # 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#fastly.jsdelivr.net/gh/Weidows/Images/post/${fileArray[$i]}\.\w\w\w#www.helloimg.com/images/${urlArray[$i]}#g" $path
    done

分割线

vscode-批量替换

Screenshot_20210313_171408_tv.danmaku.bili
-> Screenshot20210313171408

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

  • 匹配下面内容

    categories:
    - experience
    - shell
    tags:
    - experience
    - shell
  1. categories:*tags:

    行不通, 其中间含有 \n

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


尽短匹配

%SystemRoot%\system32\WBEM;%SystemRoot%\system32\WBEM;D:\Scoop\apps\vmware-workstation-pro\16.2.4-20089737\bin\;C:\WINDOWS\system32\WBEM;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\dotnet\;C:\Program Files\dotnet\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;D:\Scoop\apps\miniconda-cn\current\scripts;D:\Scoop\apps\miniconda-cn\current\Library\bin;D:\Scoop\apps\winlibs-mingw-llvm-msvcrt\current\bin;D:\Scoop\apps\go-cn\current\global_path\bin;D:\Scoop\apps\latex\current\texmfs\install\miktex\bin\x64;D:\Scoop\apps\openjdk\current\bin;D:\Scoop\apps\openjdk8-redhat\current\bin;D:\Scoop\apps\cuda11.3\current\bin;D:\Scoop\apps\cuda11.3\current\libnvvp;D:\Scoop\apps\cudnn\current\bin;D:\Scoop\apps\chocolatey_chawyehsu\current\bin;D:\Scoop\apps\nvm\current\nodejs\nodejs;D:\Scoop\apps\maven\current\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;D:\Scoop\shims;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;D:\Scoop\apps\openjdk\current\bin;D:\Scoop\apps\go-cn\current\global_path\bin;D:\Scoop\apps\nvm\current\nodejs\nodejs;D:\Scoop\apps\latex\current\texmfs\install\miktex\bin\x64;D:\Scoop\apps\openjdk8-redhat\current\bin;D:\Scoop\apps\winlibs-mingw-llvm-msvcrt\current\bin;D:\Scoop\apps\maven\current\bin;D:\Scoop\apps\miniconda-cn\current\scripts;D:\Scoop\apps\miniconda-cn\current\Library\bin;D:\Scoop\apps\cuda11.3\current\bin;D:\Scoop\apps\cuda11.3\current\libnvvp;D:\Scoop\apps\cudnn\current\bin;D:\Scoop\apps\chocolatey_chawyehsu\current\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;D:\Scoop\shims;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;C:\Users\Administrator\AppData\Local\JetBrains\Toolbox\scripts;D:\Scoop\apps\yarn\current\global\node_modules\.bin;D:\Scoop\apps\yarn\current\bin;D:\Scoop\apps\openjdk\current\bin;D:\Scoop\apps\go-cn\current\global_path\bin;D:\Scoop\apps\nvm\current\nodejs\nodejs;D:\Scoop\apps\latex\current\texmfs\install\miktex\bin\x64;D:\Scoop\apps\openjdk8-redhat\current\bin;D:\Scoop\apps\winlibs-mingw-llvm-msvcrt\current\bin;D:\Scoop\apps\maven\current\bin;D:\Scoop\apps\miniconda-cn\current\scripts;D:\Scoop\apps\miniconda-cn\current\Library\bin;D:\Scoop\apps\cuda11.3\current\bin;D:\Scoop\apps\cuda11.3\current\libnvvp;D:\Scoop\apps\cudnn\current\bin;D:\Scoop\apps\chocolatey_chawyehsu\current\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;D:\Scoop\shims;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;C:\Users\Administrator\AppData\Local\JetBrains\Toolbox\scripts

比如上面一大段, 想要匹配出来每段 D:\Scoop...;, 用 D:\\Scoop.+; 的匹配结果为一大段:

1 个匹配项
$0 -> D:\Scoop\apps\vmware-workstation-pro\16.2.4-20089737\bin\;C:\WINDOWS\system32\WBEM;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\dotnet\;C:\Program Files\dotnet\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;D:\Scoop\apps\miniconda-cn\current\scripts;D:\Scoop\apps\miniconda-cn\current\Library\bin;D:\Scoop\apps\winlibs-mingw-llvm-msvcrt\current\bin;D:\Scoop\apps\go-cn\current\global_path\bin;D:\Scoop\apps\latex\current\texmfs\install\miktex\bin\x64;D:\Scoop\apps\openjdk\current\bin;D:\Scoop\apps\openjdk8-redhat\current\bin;D:\Scoop\apps\cuda11.3\current\bin;D:\Scoop\apps\cuda11.3\current\libnvvp;D:\Scoop\apps\cudnn\current\bin;D:\Scoop\apps\chocolatey_chawyehsu\current\bin;D:\Scoop\apps\nvm\current\nodejs\nodejs;D:\Scoop\apps\maven\current\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;D:\Scoop\shims;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;D:\Scoop\apps\openjdk\current\bin;D:\Scoop\apps\go-cn\current\global_path\bin;D:\Scoop\apps\nvm\current\nodejs\nodejs;D:\Scoop\apps\latex\current\texmfs\install\miktex\bin\x64;D:\Scoop\apps\openjdk8-redhat\current\bin;D:\Scoop\apps\winlibs-mingw-llvm-msvcrt\current\bin;D:\Scoop\apps\maven\current\bin;D:\Scoop\apps\miniconda-cn\current\scripts;D:\Scoop\apps\miniconda-cn\current\Library\bin;D:\Scoop\apps\cuda11.3\current\bin;D:\Scoop\apps\cuda11.3\current\libnvvp;D:\Scoop\apps\cudnn\current\bin;D:\Scoop\apps\chocolatey_chawyehsu\current\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;D:\Scoop\shims;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;C:\Users\Administrator\AppData\Local\JetBrains\Toolbox\scripts;D:\Scoop\apps\yarn\current\global\node_modules\.bin;D:\Scoop\apps\yarn\current\bin;D:\Scoop\apps\openjdk\current\bin;D:\Scoop\apps\go-cn\current\global_path\bin;D:\Scoop\apps\nvm\current\nodejs\nodejs;D:\Scoop\apps\latex\current\texmfs\install\miktex\bin\x64;D:\Scoop\apps\openjdk8-redhat\current\bin;D:\Scoop\apps\winlibs-mingw-llvm-msvcrt\current\bin;D:\Scoop\apps\maven\current\bin;D:\Scoop\apps\miniconda-cn\current\scripts;D:\Scoop\apps\miniconda-cn\current\Library\bin;D:\Scoop\apps\cuda11.3\current\bin;D:\Scoop\apps\cuda11.3\current\libnvvp;D:\Scoop\apps\cudnn\current\bin;D:\Scoop\apps\chocolatey_chawyehsu\current\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;D:\Scoop\shims;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;

改为 D:\\Scoop.+?; 尽短匹配, 结果:

45 个匹配项
$0 -> D:\Scoop\apps\vmware-workstation-pro\16.2.4-20089737\bin\;
$1 -> D:\Scoop\apps\miniconda-cn\current\scripts;
$2 -> D:\Scoop\apps\miniconda-cn\current\Library\bin;
$3 -> D:\Scoop\apps\winlibs-mingw-llvm-msvcrt\current\bin;
$4 -> D:\Scoop\apps\go-cn\current\global_path\bin;
$5 -> D:\Scoop\apps\latex\current\texmfs\install\miktex\bin\x64;
$6 -> D:\Scoop\apps\openjdk\current\bin;
$7 -> D:\Scoop\apps\openjdk8-redhat\current\bin;
$8 -> D:\Scoop\apps\cuda11.3\current\bin;
$9 -> D:\Scoop\apps\cuda11.3\current\libnvvp;
$10 -> D:\Scoop\apps\cudnn\current\bin;
$11 -> D:\Scoop\apps\chocolatey_chawyehsu\current\bin;
$12 -> D:\Scoop\apps\nvm\current\nodejs\nodejs;
$13 -> D:\Scoop\apps\maven\current\bin;
$14 -> D:\Scoop\shims;
$15 -> D:\Scoop\apps\openjdk\current\bin;
$16 -> D:\Scoop\apps\go-cn\current\global_path\bin;
$17 -> D:\Scoop\apps\nvm\current\nodejs\nodejs;
$18 -> D:\Scoop\apps\latex\current\texmfs\install\miktex\bin\x64;
$19 -> D:\Scoop\apps\openjdk8-redhat\current\bin;
$20 -> D:\Scoop\apps\winlibs-mingw-llvm-msvcrt\current\bin;
$21 -> D:\Scoop\apps\maven\current\bin;
$22 -> D:\Scoop\apps\miniconda-cn\current\scripts;
$23 -> D:\Scoop\apps\miniconda-cn\current\Library\bin;
$24 -> D:\Scoop\apps\cuda11.3\current\bin;
$25 -> D:\Scoop\apps\cuda11.3\current\libnvvp;
$26 -> D:\Scoop\apps\cudnn\current\bin;
$27 -> D:\Scoop\apps\chocolatey_chawyehsu\current\bin;
$28 -> D:\Scoop\shims;
$29 -> D:\Scoop\apps\yarn\current\global\node_modules\.bin;
$30 -> D:\Scoop\apps\yarn\current\bin;
$31 -> D:\Scoop\apps\openjdk\current\bin;
$32 -> D:\Scoop\apps\go-cn\current\global_path\bin;
$33 -> D:\Scoop\apps\nvm\current\nodejs\nodejs;
$34 -> D:\Scoop\apps\latex\current\texmfs\install\miktex\bin\x64;
$35 -> D:\Scoop\apps\openjdk8-redhat\current\bin;
$36 -> D:\Scoop\apps\winlibs-mingw-llvm-msvcrt\current\bin;
$37 -> D:\Scoop\apps\maven\current\bin;
$38 -> D:\Scoop\apps\miniconda-cn\current\scripts;
$39 -> D:\Scoop\apps\miniconda-cn\current\Library\bin;
$40 -> D:\Scoop\apps\cuda11.3\current\bin;
$41 -> D:\Scoop\apps\cuda11.3\current\libnvvp;
$42 -> D:\Scoop\apps\cudnn\current\bin;
$43 -> D:\Scoop\apps\chocolatey_chawyehsu\current\bin;
$44 -> D:\Scoop\shims;

分割线

sed-替换反斜杠

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

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

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

分割线

删除匹配多行区域

起因是写的 host 更新脚本有时会因为网络原因出错, 我想删除<html></html> 这部分

# pollen
127.0.0.1 pollen-svc

# simswap.service
127.0.0.1 simswap-service-svc

<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.14.0 (Ubuntu)</center>
</body>
</html>
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.14.0 (Ubuntu)</center>
</body>
</html>
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.14.0 (Ubuntu)</center>
</body>
</html>
# GitHub520 Host Start
140.82.113.3 alive.github.com
140.82.113.3 live.github.com
35.91.180.210 github.githubassets.com
140.82.113.3 central.github.com
35.160.167.22 desktop.githubusercontent.com
140.82.113.3 assets-cdn.github.com
54.218.63.240 camo

多行匹配-替换模式

不知道为啥, 按道理 regex 没问题, 但是不起效…

sed -i "s/<[\w\W]*>/ /g" 1.txt

字符匹配-行删除模式

sed -i "/</d" 1.txt

分割线

报错

/d/Scoop/apps/git-all/current/usr/bin/sed: cannot rename ./sedLO8z5J: Invalid cross-device link

跨盘操作不允许替换文件 [4]

分割线

借物表

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

[2]: 正则表达式如何匹配多行的所有任意字符

[3]: sed 删除文件中含有特定字符(串)的行_51CTO 博客_sed 删除指定行

[4]: sed 修改文件不成功