简体   繁体   English

Sublime Text 2 中的正则表达式搜索替换

[英]Regular expression search replace in Sublime Text 2

I'm looking to do search replace with regular expressions in Sublime Text 2. The documentation on this is rather anemic.我希望在 Sublime Text 2 中用正则表达式进行搜索替换。这方面的文档相当贫乏。 Specifically, I want to do a replace on groups, so something like converting this text:具体来说,我想对组进行替换,例如转换此文本:

Hello my name is bob

And this search term:这个搜索词:

Find what: my name is (\w)+查找内容: my name is (\w)+

Replace with: my name used to be $(1)替换为: my name used to be $(1)

The search term works just fine but I can't figure out a way to actually do a replace using the regexp group.搜索词工作得很好,但我想不出一种方法来实际使用正则表达式组进行替换。

Usually a back-reference is either $1 or \1 (backslash one) for the first capture group (the first match of a pattern in parentheses), and indeed Sublime supports both syntaxes.对于第一个捕获组(括号中模式的第一个匹配项),反向引用通常是$1\1 (反斜杠),实际上 Sublime 支持这两种语法。 So try:所以尝试:

my name used to be \1

or要么

my name used to be $1

Also note that your original capture pattern:另请注意您的原始捕获模式:

my name is (\w)+

is incorrect and will only capture the final letter of the name rather than the whole name.是不正确的,只会捕获名称的最后一个字母而不是整个名称。 You should use the following pattern to capture all of the letters of the name:您应该使用以下模式来捕获名称的所有字母:

my name is (\w+)

By the way, in the question above:顺便说一句,在上面的问题中:

For:为了:

Hello, my name is bob

Find part:查找部分:

my name is (\w)+

With replace part:替换部分:

my name used to be \1

Would return:会返回:

Hello, my name used to be b

Change find part to:将查找部分更改为:

my name is (\w+)

And replace will be what you expect:替换将是你所期望的:

Hello, my name used to be bob

While (\w)+ will match "bob", it is not the grouping you want for replacement.虽然 (\w)+ 将匹配“bob”,但它不是您要替换的分组。

Important: Use the ( ) parentheses in your search string重要提示:在搜索字符串中使用( )括号

While the previous answer is correct there is an important thing to emphasize!虽然前面的答案是正确的,但有一点需要强调! All the matched segments in your search string that you want to use in your replacement string must be enclosed by ( ) parentheses , otherwise these matched segments won't be accessible to defined variables such as $1 , $2 or \1 , \2 etc.您要在替换字符串中使用的搜索字符串中的所有匹配段都必须( )圆括号括起来,否则这些匹配段将无法访问已定义的变量,例如$1$2\1\2等。

For example we want to replace 'em' with 'px' but preserve the digit values:例如,我们想用 'px' 替换 'em' 但保留数字值:

    margin: 10em;  /* Expected: margin: 10px */
    margin: 2em;   /* Expected: margin: 2px */
  • Replacement string: margin: $1px or margin: \1px替换字符串: margin: $1pxmargin: \1px
  • Search string (CORRECT): margin: ([0-9]*)em // with parentheses搜索字符串(正确): margin: ([0-9]*)em // 带括号
  • Search string (INCORRECT): margin: [0-9]*em搜索字符串(错误): margin: [0-9]*em

CORRECT CASE EXAMPLE: Using margin: ([0-9]*)em search string (with parentheses).正确案例示例:使用margin: ([0-9]*)em搜索字符串(带括号)。 Enclose the desired matched segment (eg $1 or \1 ) by ( ) parentheses as following:( )括号将所需的匹配段(例如$1\1 )括起来,如下所示:

  • Find: margin: ([0-9]*)em (with parentheses)查找: margin: ([0-9]*)em (带括号)
  • Replace to: margin: $1px or margin: \1px替换为: margin: $1pxmargin: \1px
  • Result:结果:
    margin: 10px;
    margin: 2px;

INCORRECT CASE EXAMPLE: Using margin: [0-9]*em search string (without parentheses).不正确的案例示例:使用margin: [0-9]*em搜索字符串(不带括号)。 The following regex pattern will match the desired lines but matched segments will not be available in replaced string as variables such as $1 or \1 :以下正则表达式模式将匹配所需的行,但匹配的段将无法在替换字符串中作为$1\1等变量使用:

  • Find: margin: [0-9]*em (without parentheses)查找: margin: [0-9]*em (不带括号)
  • Replace to: margin: $1px or margin: \1px替换为: margin: $1pxmargin: \1px
  • Result:结果:
    margin: px; /* `$1` is undefined */
    margin: px; /* `$1` is undefined */

Here is a visual presentation of the approved answer.这是已批准答案的直观呈现。

在此处输入图像描述

Note that if you use more than 9 capture groups you have to use the syntax ${10} .请注意,如果您使用超过 9 个捕获组,则必须使用语法${10}

$10 or \10 or \{10} will not work. $10\10\{10}不起作用

Looking at Sublime Text Unofficial Documentation's article on Search and Replace , it looks like +(.+) is the capture group you might want... but I personally used (.*) and it worked well.查看Sublime Text 非官方文档关于搜索和替换的文章,看起来+(.+)是您可能想要的捕获组......但我个人使用(.*)并且效果很好。 To REPLACE in the way you are saying, you might like this conversation in the forums , specifically this post which says to simply use $1 to use the first captured group.要按照您所说的方式替换,您可能会喜欢论坛中的这个对话,特别是这篇文章,它说只需使用$1即可使用第一个捕获的组。

And since pictures are better than words...而且由于图片胜于文字...

Before:前: 在查找/替换之前

After:后: 查找/替换后

I had similar problem:我有类似的问题:

Many strigs with this pattern like in Don Jo s6 went (correct was Don Jo went)许多像 Don Jo s6那样具有这种模式的 strigs 去了(正确的是 Don Jo 去了)

I used this tool: https://coding.tools/regex-replace我使用了这个工具: https://coding.tools/regex-replace

Regular Expression: ([az])6正则表达式: ([az])6

Replace To: $1é替换为: $1é

Click on the "Regex Replace" Button.单击“正则表达式替换”按钮。 Result:结果:

Don Jo went去了

It also works in Google Sheets.它也适用于 Google 表格。

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

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