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. 搜索字词效果很好,但是我找不到使用regexp组进行替换的方法。


#1楼

参考:https://stackoom.com/question/natK/Sublime-Text-中的正则表达式搜索替换


#2楼

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

#3楼

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”,但它不是您要替换的分组。


#4楼

Use the ( ) parentheses in your search string 在搜索字符串中使用()括号

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 reachable with variables such as $1, $2,...nor \\1, \\2,.. and etc. 您要在替换字符串中使用的搜索字符串中所有匹配的句段都必须用()括号括起来 ,否则这些匹配的句段将无法用$ 1,$ 2,... nor \\ 1,\\ 2,..等

EXAMPLE: 例:

We want to replace 'em' with 'px' but preserve the number values: 我们想用“ px”代替“ em”,但保留数字值:

margin: 10em
margin: 2em

So we use the margin: $1px as the replacement string. 因此,我们使用margin: $1px作为替换字符串。


CORRECT: Enclose the desired $1 matched segment by ( ) parentheses as following: 正确:将所需的$1匹配的段用( )括起来,如下所示:

FIND: margin: ([0-9]*)em (With parentheses) 查找: margin: ([0-9]*)em (带括号)

REPLACE TO: margin: $1px 替换为: margin: $1px

RESULT: 结果:

margin: 10px
margin: 2px

WRONG: The following regex pattern will match the desired lines but matched segments will not be available in replaced string as variables such as $1 : 错误:以下正则表达式模式将匹配所需的行,但匹配的段将在替换的字符串中不可用,如$1变量:

FIND: margin: [0-9]*em (Without parentheses) 查找: margin: [0-9]*em (不带括号)

REPLACE TO: margin: $1px 替换为: margin: $1px

RESULT: ( $1 is undefined) 结果:( $1未定义)

margin: px
margin: px

#5楼

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}无法正常工作。


#6楼

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

在此处输入图片说明

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值