简体   繁体   English

使用RegEx搜索和替换Notepad ++选择中的数字

[英]Using RegEx to search and replace a number in a Notepad++ selection

I just learned about regex and it's still quite confusing to me. 我刚刚了解了正则表达式,但仍然让我感到困惑。

For a start, I'm having a file in Notepad++ with 500k lines. 首先,我在Notepad ++中有一个500k行的文件。 At about line 394900 is some static content that needs to be replaced. 在大约394900行处,有一些静态内容需要替换。 It's not that much to do manually, but just for learning purpusses I'd like to do this via RegEx. 手动完成的工作不多,但只是为了学习目标,我想通过RegEx进行此操作。

The lines contain the following strings while X is a RandomNumber: {x 0.xxxxx xx.xxx} Now I have to replace the second number of each line before the .xxxxx 当X是RandomNumber时,这些行包含以下字符串:{x 0.xxxxx xx.xxx}现在,我必须替换.xxxxx之前每行的第二个数字

So to make it short: {x 0.xxxxx xx.xxx} has to be replaced with {x 8.xxxxx xx.xxx} in each line. 简而言之:每行必须将{x 0.xxxxx xx.xxx}替换为{x 8.xxxxx xx.xxx}。

What would be the easiest way to do this via regex? 通过正则表达式最简单的方法是什么?

Using \\d to represent a digit: 使用\\d代表数字:

Replace (\\{\\d )\\d(\\.\\d{5} \\d{2}\\.\\d{3}\\}) 替换(\\{\\d )\\d(\\.\\d{5} \\d{2}\\.\\d{3}\\})

with \\18\\2 (first group, then the digit 8 , followed by the second group). \\18\\2 (第一个组,然后是数字8 ,然后是第二组)。

If explicit quantifiers (eg \\d{3} ) are not supported, use \\d+ instead (ie one or more digits): 如果不支持显式量词(例如\\d{3} ),请改用\\d+ (即一位或多位数字):

Without explicit quantifiers (for Npp version < 6): 没有显式量词(对于Npp版本<6):

Replace (\\{\\d )\\d(\\.\\d+ \\d+\\.\\d+\\}) with \\18\\2 . (\\{\\d )\\d(\\.\\d+ \\d+\\.\\d+\\})替换为\\18\\2

You could use a positive lookahead based regex like below, 您可以使用如下所示的基于正向超前的正则表达式,

\d+(?=\.\d{5}\s)

Then replace the matched number with 8 . 然后将匹配的数字替换为8 It matches the number only if it's followed by a dot then a 5 digit number and a space. 仅当其后跟一个点,5位数字和一个空格时,它才与数字匹配。

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM