【问题标题】:regex: continuous numbering replace正则表达式:连续编号替换
【发布时间】:2014-04-29 06:58:30
【问题描述】:

我有一个重复单词“文章”的文本,我需要使用记事本 ++ 将每个单词替换为一个数字,从“1”开始,然后是下一个实例“2”......等等。 {\d+[+1]}

例如:

this is article and this is another article.  Here is an article etc.

变成:

this is 1 and this is another 2.  Here is an 3 etc.

【问题讨论】:

  • notepad++ 无法做到这一点。然而,使用编程语言、sed 或 awk 可以很容易地完成。
  • 我从来没有使用过这些。你能建议一种方法来做到这一点并下载 Windows 所需的工具吗?谢谢

标签: regex replace numbers notepad++


【解决方案1】:

是的,你可以,但这很棘手。 假设您的文件中没有任何 #,请将每一行替换为下一行(替换全部和多次):

(?<!#)(#*article)([^#]*?)((?=\1))
\1\2#\3

所以这给出:this is article and this is another #article. Here is an ##article etc.。继续:

article
\#

所以你有: this is # and this is another ##. Here is an ### etc.

现在每篇文章都映射到其位置磅数的 n 倍。转换为base 10,需要交替重复执行以下操作(假设原文中没有出现%)

#{10}
%

%{10}
#

以此类推,直到您既不能替换第一个也不能替换第二个。然后你可以这样做:

(#|%)\1{8}
9

(#|%)\1{7}
8

(#|%)\1{6}
7

(#|%)\1{5}
6

(#|%)\1{4}
5

(#|%)\1{3}
4

(#|%)\1{2}
3

(#|%)\1{1}
2

(#|%)
1

然后瞧! this is 1 and this is another 2. Here is an 3 etc.如果你的文档有n篇文章,第一次操作需要O(n)次点击“全部替换”,第二次O(log(n)),第三次O(1),所以总时间实际上是 O(n),这是您所期望的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    相关资源
    最近更新 更多