regex - 如何用动态数量的空格替换子字符串

标签 regex notepad++

我想使用 Notepad++ 将一列数字的小数位替换为等量的空格。

例如,这个输入(竖线是故意的)

|    1.23|
|  45.678|
|901.2345|

会变成

|    1   |
|  45    |
|901     |

基本上,点和小数位已替换为动态数量的空格:第一行 2+1 个空格,第二行 3+1 个空格,第三行 4+1 个空格。

我看过一些不同的帖子,例如 this并尝试过类似的操作:\.\d+ 替换为 \s+,但没有成功。

最佳答案

  • Ctrl+H
  • 查找内容:\.(\d)?(\d)?(\d)?(\d)?
  • 替换为: (?1 )(?2 )(?3 )(?4 )
  • 检查 环绕
  • 检查 正则表达式
  • 全部替换

说明:

\.          # a dot
(\d)?       # optional group 1, 1 digit
(\d)?       # optional group 2, 1 digit
(\d)?       # optional group 3, 1 digit
(\d)?       # optional group 4, 1 digit
and so on

替换:

            # a space
(?1 )       # if group 1 exists, add a space
(?2 )       # if group 2 exists, add a space
(?3 )       # if group 3 exists, add a space
(?4 )       # if group 4 exists, add a space
and so on

屏幕截图(之前):

enter image description here

屏幕截图(之后):

enter image description here

关于regex - 如何用动态数量的空格替换子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69316876/

相关文章:

regex - Perl 正则表达式多行匹配到哈希

python - 使用 python 的正则表达式不起作用

c# - 更改文本文件编码

java - 对于长度超过 30 个字符的字符串,正则表达式模式匹配需要很长时间

regex - 用于在 ColdFusion 9 中删除字符串中数值周围的引号的正则表达式

regex - 如何用正则表达式替换 WebStorm/PhpStorm

php - NotePad++ 中 CSS(样式)的自动完成

Javascript 文件崩溃并转换为 '' NUL NUL NUL"notepad++

regex - 用于匹配 or 的正则表达式

java - Java 程序为 VB 应用程序生成的文件的编码问题