在多行中插入一些文本 32 个字符,然后行结束


Inserting some text into multiple lines 32 characters before the line ends

所以我需要在 22245 行文件的每一行中添加'0',所有值都不同,所以查找和替换不起作用,我想知道是否有正则表达式方法或我可以使用记事本++从每行末尾插入 32 个字符?

或者也许是不同的程序或方式? 我知道 php 脚本允许我从开头或结尾插入可变数量的空格,但这似乎是不必要的努力。

使用记事本++,您可以使用捕获组(( ... )(,行尾锚点($(定量词{32}表示32个字符,通配符.和替换框中的替换反向引用,如下所示:

找到:

(.{32})$

替换为:

0$1

或者使用积极的前瞻性,发现:

(?=.{32}$)

替换为:

0

确保已选中正则表达式搜索框。

如果要在特定行插入单词/行,可以使用以下解决方案。它将整个文件内容读入一个数组,并使用array_splice()将新单词插入其中:

// read the file into an array
$lines = file('file.txt');
// set the word and position to be inserted
$wordToBeInserted = 'foo';
$pos = 32; 
// add the word into the array and write it back
array_splice($lines, $pos-1, 0, array("$wordToBeInserted'n"));
// write it back 
file_put_contents('file.txt', implode('', $lines));
相关文章: