简体   繁体   English

正则表达式:保持在同一行中多次发现相同模式

[英]Regex: keep same pattern found multiple times in same line

Is it possible with notepad++ (or maybe from linux bash shell) to create multiple lines from a pattern found , as many times as the pattern is found? 是否可以使用notepad ++(或可能从linux bash shell中)从找到的模式创建多行,并尽可能多地找到该模式? The pattern in this case is val=[0-9]+ 这种情况下的模式是val = [0-9] +

For example: 例如:

input line: other stuff,val=3234,val=123,val=678 输入行: other stuff,val=3234,val=123,val=678

output lines: 输出线:

val=3234
val=123
val=678

Press Ctrl + H Ctrl + H

Enable Regular Expression. 启用正则表达式。

Regex explanation: ,(val=\\d+) - match comma and then val=[0-9] recurring, with val=\\d+ captured as Group 1 . 正则表达式说明: ,(val=\\d+) -匹配逗号,然后重复val=[0-9] ,其中val=\\d+被捕获为第1组 In Notepad++, you can access Group 1 contents as \\1 . 在Notepad ++中,您可以将\\ 1组内容作为\\ 1进行访问。

So replace your regex result with newline ( \\n ) + Group 1 contents ( \\1 ). 因此,将您的正则表达式结果替换为换行( \\ n )+组1内容( \\ 1 )。

在此处输入图片说明

Result: 结果:

other stuff
val=3234
val=123
val=678

If you want other stuff to be removed, then you first need to capture it via following regex: ^[^,]+ and Replace with nothing, and then run regex ,(val=\\d+) and replace with \\1\\n . 如果要删除other stuff ,则首先需要通过以下正则表达式捕获它: ^[^,]+并替换为空,然后运行regex ,(val=\\d+)并替换为\\1\\n Then result will be: 然后结果将是:

val=3234
val=123
val=678

You can slightly improve on @vs97 answer by using the regex 您可以使用正则表达式略微改善@ vs97答案

[^,]*,(val=\d+)

and replacing with \\1\\n . 并替换为\\1\\n This will deal with getting rid of the 'other stuff` without requiring a separate replace operation. 这将解决摆脱“其他东西”的问题,而无需单独的替换操作。

Demo on regex101 regex101上的演示

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

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