【问题标题】:Inserting some text into multiple lines 32 characters before the line ends [closed]在行结束前将一些文本插入多行 32 个字符[关闭]
【发布时间】:2013-10-10 18:29:25
【问题描述】:

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

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

【问题讨论】:

    标签: php regex notepad++


    【解决方案1】:

    使用notepad++,您可以使用捕获组(( ... )),行尾锚($)限定量词{32}表示32个字符,通配符.和replace中的replace反向引用像这样的盒子:

    查找:

    (.{32})$
    

    替换为:

    0$1
    

    或者使用积极的前瞻,找到:

    (?=.{32}$)
    

    替换为:

    0
    

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

    【讨论】:

    • @salty 不客气 :)
    【解决方案2】:

    如果要在特定行插入单词/行,可以使用以下解决方案。它将整个文件内容读取到一个数组中,并使用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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      相关资源
      最近更新 更多