简体   繁体   English

正则表达式记事本++和组

[英]Regex notepad++ and groups

I have the following data in my file: 我的文件中包含以下数据:

234xt_
yad42_
23ft3_
45gdw_
...

Where the _ means a space. _表示空间。 Using Notepad++ I want to rewrite it to be: 使用Notepad ++我想将其重写为:

'234xt',
'yad42',
'23ft3',
'45gdw'

I am using the following regex in the "Find what" (^\\w+)\\s*\\n 我在“查找内容” (^\\w+)\\s*\\n使用以下正则表达式

And in the "Replace with" field $0 , 并在“替换为”字段$0

But it is not working as expected. 但它没有按预期工作。

You may use 你可以用

^(\w+) $

or 要么

^(\w+)\h$

And replace with '$1', . 并替换为'$1',

^ will match the start of a line, (\\w+) will place one or more letters, digits or underscores into Group 1 (that you may access via $1 or \\1 backreference in the replacement pattern), and then a space or \\h will match a space or any horizontal whitespace, and then $ will assert the position at the end of the line. ^将匹配行的开头, (\\w+)会将一个或多个字母,数字或下划线放入第1组(您可以通过替换模式中的$1\\1反向引用访问),然后是空格或\\h将匹配空格或任何水平空格,然后$将在该行的末尾断言位置。

If the (white)spaces can go missing add the appropriate quantifier after the space or \\h : \\h* will match 0 or more whitespaces and \\h? 如果(白色)空格可以丢失,请在空格后添加适当的量词,或者\\h\\h*将匹配0个或更多空格和\\h? will match 1 or 0. 将匹配1或0。

Settings & demo: 设置和演示:

在此输入图像描述

您应该使用\\1而不是$0查看文档中的示例。

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

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