使用找到的文本替换另一个文本

时间:2013-07-10 12:50:35

标签: regex notepad++

我想找到4000到4999之间的所有数字,并将前导4替换为7。

例如:

4000 -> 7000
4015 -> 7015
4987 -> 7987

我尝试用4\d\d\d\d替换7\1,但它不起作用。

3 个答案:

答案 0 :(得分:3)

搜索(?<!\d)4(\d{3})(?!\d)并替换为7\1

<强>解释

(?<!\d)     # Negative lookbehind: check if there is no digit preceding 4
4           # match 4
(           # start group 1
    \d{3}   # match 3 digits
)           # end group 1
(?!\d)      # Negative lookahead: check if there is no digit following the 3 digits

替换:\1指的是第1组。

enter image description here

虽然蒂姆的解决方案更好:p

答案 1 :(得分:3)

使用正则表达式替换模式搜索\b4(\d{3})\b并替换为7\1

word boundaries确保您不会意外地与1400040000匹配。

答案 2 :(得分:1)

notepad++

下面尝试

搜索4(\d\d\d)并替换为7\1

相关问题