Sublime Text 2 中的正则表达式搜索替换 [英] Regular expression search replace in Sublime Text 2

查看:45
本文介绍了Sublime Text 2 中的正则表达式搜索替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 Sublime Text 2 中用正则表达式进行搜索替换.关于这个的文档相当贫乏.具体来说,我想对组进行替换,因此类似于转换此文本:

I'm looking to do search replace with regular expressions in Sublime Text 2. The documentation on this is rather anemic. Specifically, I want to do a replace on groups, so something like converting this text:

你好,我叫鲍勃

还有这个搜索词:

查找内容:我的名字是 (\w)+

替换为:我以前的名字是 $(1)

搜索词工作正常,但我无法找到使用正则表达式组实际进行替换的方法.

The search term works just fine but I can't figure out a way to actually do a replace using the regexp group.

推荐答案

通常第一次捕获的反向引用是 $1\1(反斜杠一个)group(括号中模式的第一个匹配项),实际上 Sublime 支持这两种语法.所以试试:

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. So try:

my name used to be \1

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+)

这篇关于Sublime Text 2 中的正则表达式搜索替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆