简体   繁体   English

Notepad ++查找正则表达式,并替换为正则表达式

[英]Notepad++ find regex and also replace with regex

I have tons of this: 我有很多这样的:

(278, 191, 825, 824, 3, <other code> ),
(282, 185, 326, 327, 4, <other code> ),
(284, 184, 545, 546, 3, <other code> ),
.
.
.
(314, 185, 340, 341, 4, <other code> ),

I want to add 0 at the end of the first number for each line 我想在每行的第一个数字的末尾添加0

eg: (278, //rest of code ), to (2780, //rest of code ), 例如: (278, //rest of code ), to (2780, //rest of code ),

I'd managed to find match using regex: \\(.+\\, 1 will highlight eg: (278, 1 but dont know how to replace it to (2780, 1 for the rest of other numbers too 我设法使用正则表达式找到匹配项: \\(.+\\, 1将突出显示例如: (278, 1但不知道如何将其替换为(2780, 1对于其他数字也是如此

You should be able to enter for "Find what": 您应该可以输入“查找内容”:

\(([0-9]+)\,

For "Replace with": 对于“替换为”:

\(\10,

The "\\1" is the contents of the first capture group. “ \\ 1”是第一个捕获组的内容。

This works for me in Notepad++ using your example. 在使用您的示例的Notepad ++中,这对我有用。

Edit 编辑

If you need a regex reference this is a pretty good one: https://stackoverflow.com/a/22944075/1684623 如果您需要正则表达式参考,这是一个很好的参考: https : //stackoverflow.com/a/22944075/1684623

Of course you could make this more robust/cleaner by matching the start of line ( ^ ), using \\d instead of [0-9] , etc. 当然,您可以通过匹配行的开头( ^ ),使用\\d而不是[0-9]等来使此功能更强大/更清晰。

If your file has consistently the format for each line of left bracket followed by 3 decimals and a comma, then an incredibly easy way to do this in Notepad++ is to use the "Column Editor" mode. 如果文件的左括号每一行的格式一致,后跟3个小数和一个逗号,则在Notepad ++中执行此操作的一种非常简单的方法是使用“列编辑器”模式。

Do this by holding down the "Alt" key, and then click your left mouse button to highlight the comma all the way from top to bottom of the file, then once you've selected all the commas, press Alt+C to get the "Column / Multi-Selection Editor" to appear. 为此,请按住“ Alt”键,然后单击鼠标左键以从文件的顶部到底部一直突出显示逗号,然后在选择所有逗号后,按Alt + C获取出现“列/多选编辑器”。 Then enter Text to Insert as "0,", and click OK. 然后输入要插入的文本作为“ 0”,然后单击“确定”。

See my screenshot below: 请参阅下面的屏幕截图:

记事本列选择

This should take you literally seconds. 这实际上将花费您几秒钟。 I've used it many times, and it will save you a lot of grief when doing tedious tasks like this. 我已经使用了很多次,在执行这样繁琐的任务时,它将为您节省很多麻烦。

You need to insert ( and ) in the source regex, around the part you want to reuse, and insert \\1 in the target regex to get it put there. 您需要在要重用的部分周围的源正则表​​达式中插入() ,并在目标正则表达式中插入\\1 ,以将其放置在那里。

I can't say if your editor supports this, but that is the usual way 我不能说您的编辑器是否支持此功能,但这是通常的方法

You're going to need a capturing group 您将需要一个捕获小组

You then add brackets around the bit you want to use, and use \\1 in your replace expression to use it. 然后,在要使用的位周围添加方括号,并在替换表达式中使用\\1来使用它。

Something like ^\\(([0-9]+), as your find expression will do the trick, but your desired replace expression introduces a new problem. You'd want (\\10, , but that may attempt to get the 10th capturing group, not the first one followed by a zero. That said, apparently many implementations do what you want, so this should suffice. Read more about that problem here . 类似于^\\(([0-9]+),因为您的find表达式可以解决问题,但是所需的replace表达式引入了一个新问题。您想要(\\10, ,,但这可能会尝试获得第10个捕获组,而不是第一个捕获组,后跟一个零。也就是说,很显然,许多实现都可以实现您想要的,所以就足够

Using Notepad++ replace: 使用记事本++替换:

Find:         ^(\()(\d+)

Replace with: \1\20

\\1 is the first match group; \\1是第一个匹配组; \\2 is the second match group. \\2是第二个匹配组。

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

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