【问题标题】:Notepad++ incrementally replaceNotepad++ 增量替换
【发布时间】:2011-11-28 00:06:09
【问题描述】:

假设我想要 10 行数据,但我想要为每行或每条数据增加一个值。如何增加该值?

例如....如果我有这些行,是否有正则表达式替换 id 值以增加?

<row id="1" />
<row id="1" />
<row id="1" />
<row id="1" />
<row id="1" />

--- 这是我希望它看起来的样子...(如果第一行的 id 上升一个就可以了)

<row id="1" />
<row id="2" />
<row id="3" />
<row id="4" />
<row id="5" />

【问题讨论】:

  • Notepad++ 替换不做数学运算,正则表达式也不做。
  • 如果有可能,那是非常困难的。正则表达式中没有数字的概念(因此没有 +1),并且在匹配正则表达式时没有简单的方法来累积数据(因此没有“下一个”匹配)。如果您真的需要自动化,XSLT 可能会在这里为您提供帮助。
  • @Amber Notepad++ 的正则表达式功能有限。
  • @Karolis 我非常清楚这一点。我只是说正则表达式(天生)和 Notepad++ 的搜索和替换 using 正则表达式的实现都不支持替换数学。
  • 虽然正则表达式被提到(并标记)为一种可能的方法,但 OP 似乎不太关心特定方法,而只是知道 Notepad++ 是否可以完成任务...... voithos 的回答完美地解决了这个问题。

标签: regex notepad++ sequential


【解决方案1】:

不确定正则表达式,但您可以在 Notepad++ 中执行此操作,尽管它不是很灵活。

在您提供的示例中,按住 Alt 并选择您要更改的数字列。然后转到Edit-&gt;Column Editor 并在出现的窗口中选择Number to Insert 单选按钮。然后指定您的初始数字和增量,然后单击“确定”。它应该写出递增的数字。

注意:这也适用于Multi-editing 功能(在按住 Ctrl 键的同时选择多个位置)。

然而,这远不及大多数人认为有用的灵活性。 Notepad++ 很棒,但如果你想要一个真正强大的编辑器,可以轻松完成这样的事情,我会说使用 Vim。

【讨论】:

  • +1 既用于晦涩的列功能,又指出 vim 是一个更强大的编辑器。 ;)
  • Notepad++ 太棒了,如果我们有一个和这个一样强大的在线文本编辑器,那就太好了。
  • 对我来说是:使用 Alt+Shift 选择列,然后执行要插入的数字。完美运行
  • 谢谢!这个功能很棒。另外,我可以通过双击来选择该列。
  • 太棒了!在不得不准备 300 个插入语句时救了我的命,
【解决方案2】:

我今天正在寻找相同的功能,但无法在 Notepad++ 中执行此操作。但是,我们有 TextPad 来拯救我们。它对我有用。

在 TextPad 的替换对话框中,打开正则表达式;那你可以试试替换

<row id="1"/>

<row id="\i"/>

查看此链接以了解 TextPad 的更多惊人替换功能 - http://sublimetext.userecho.com/topic/106519-generate-a-sequence-of-numbers-increment-replace/

【讨论】:

  • 我想如果我真正注意它会有所帮助。最终安装了 SublimeText。 :) 谢谢。
  • 但请确保您点击了“全部替换”按钮。 “替换下一个”不会增加计数器。花了几个小时才弄清楚这一点。 \i 替换为从 1 开始,加 1 的数字。 \i(10) 替换为从 10 开始,加 1 的数字。 \i(0,10) 替换为从 0 开始,加 10 的数字。 \i( 100,-10) 替换为从 100 开始,递减 -10 的数字。
【解决方案3】:

我在超过 250 行时遇到了同样的问题,我是这样做的:

例如:

<row id="1" />
<row id="1" />
<row id="1" />
<row id="1" />
<row id="1" />

您将光标放在“1”之后,然后单击 alt + shift 并使用向下箭头开始下降直到到达底线现在您看到一组选择单击擦除以擦除每行上的数字 1同时转到Edit -&gt; Column Editor并选择Number to Insert,然后将1放入初始数字字段,1放入按字段递增并检查零数字并单击ok

恭喜你做到了:)

【讨论】:

    【解决方案4】:

    由于真实答案有限,我将分享此解决方法。对于真的像你的例子这样简单的情况,你倒着做......

    从这里

    1
    2
    3
    4
    5
    

    \r\n 替换为 " /&gt;\r\n&lt;row id=",您将获得 90% 的成功

    1" />
    <row id="2" />
    <row id="3" />
    <row id="4" />
    <row id="5
    

    或者是一种类似的方式,您可以使用 excel/电子表格来破解数据。只需将原始数据拆分为列并根据需要操作值。

    |   <row id="   |   1   |   " />    |
    |   <row id="   |   1   |   " />    |
    |   <row id="   |   1   |   " />    |
    |   <row id="   |   1   |   " />    |
    |   <row id="   |   1   |   " />    |
    

    很明显的东西,但它可能会帮助做奇怪的一次性黑客工作的人节省一些击键。

    【讨论】:

    • 在没有. matches newline 的正则表达式模式下用&lt;row id="\1" /&gt; 替换(.*) 将让你100% 到达那里。
    【解决方案5】:

    http://docs.notepad-plus-plus.org/index.php/Inserting_Variable_Text

    Notepad++ 配备了一个编辑 -> 列“Alt+C”编辑器,它可以以两种不同的方式处理矩形选择:Coledit.png 在插入点的列(也称为插入符号)的每一行(包括当前行并跟随当前行)插入一些固定文本。最初选择的文本保持不变。 如图所示,可以以相同的方式插入一系列线性数字。应提供起始值和增量。左填充是一个选项,可以以 2、8、10 或 16 为基数输入数字 - 这也是计算值的显示方式,填充基于最大值。

    【讨论】:

    • 如果您在帖子中附上图片会更好,以避免阅读您的答案时感到困惑
    【解决方案6】:


    上面建议的解决方案只有在数据对齐时才有效。
    使用 PythonScript Notepad++ 插件查看链接中的解决方案,效果很好!

    stackoverflow Find/Replace but Increment Value

    【讨论】:

      【解决方案7】:

      如果您将值存储在文件 input.txt 中,您可以通过正则表达式和 foreach 循环使用 Powershell:

      $initialNum=1; $increment=1; $tmp = Get-Content input.txt | foreach {  $n = [regex]::match($_,'id="(\d+)"').groups[1
      ].value; if ($n) {$_ -replace "$n", ([int32]$initialNum+$increment); $increment=$increment+1;} else {$_}; }
      

      之后,您可以使用$tmp &gt; result.txt 将$tmp 存储在文件中。这不需要数据在列中。

      【讨论】:

        【解决方案8】:

        (张贴以防有人可能使用它)。

        我正在寻找一个比 OP 更复杂的问题的解决方案 - 用递增的数字替换每一次出现的数字

        例如替换这样的东西:

        <row id="1" />
        <row id="2" />
        <row id="1" />
        <row id="3" />
        <row id="1" />
        

        通过这个:

        <row id="2" />
        <row id="3" />
        <row id="2" />
        <row id="4" />
        <row id="2" />
        

        在网上找不到解决方案,所以我用 groovy 编写了自己的脚本(有点难看,但确实可以):

         /**
         * <p> Finds words that matches template and increases them by 1.
         * '_' in word template represents number.
         *
         * <p> E.g. if template equals 'Row="_"', then:
         * ALL Row=0 will be replaced by Row="1"
         * All Row=1 will be replaced by Row="2"
         * <p> Warning - your find template might not work properly if _ is the last character of it
         * etc.
         * <p> Requirments:
         * - Original text does not contain tepmlate string
         * - Only numbers in non-disrupted sequence are incremented and replaced
         * (i.e. from example below, if Row=4 exists in original text, but Row=3 not, than Row=4 will NOT be 
         * replaced by Row=5)
         */
        def replace_inc(String text, int startingIndex, String findTemplate) {
            assert findTemplate.contains('_') : 'search template needs to contain "_" placeholder'
            assert !(findTemplate.replaceFirst('_','').contains('_')) : 'only one "_" placeholder is allowed'
            assert !text.contains('_____') : 'input text should not contain "______" (5 underscores)'
            while (true) {
                findString = findTemplate.replace("_",(startingIndex).toString())
                if (!text.contains(findString)) break;
                replaceString = findTemplate.replace("_", "_____"+(++startingIndex).toString())
                text = text.replaceAll(findString, replaceString)
            }
            return text.replaceAll("_____","") // get rid of '_____' character
        }
        
        // input
        findTemplate = 'Row="_"'
        path = /C:\TEMP\working_copy.txt/
        startingIndex = 0
        
        // do stuff
        f = new File(path)
        outText = replace_inc(f.text,startingIndex,findTemplate)
        println "Results \n: " + outText
        f.withWriter { out -> out.println outText }
        println "Results written to $f"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-09-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-21
          • 2017-01-27
          • 1970-01-01
          相关资源
          最近更新 更多