本文翻译自: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. 这是批准答案的直观展示。

Sublime Text 2中的正则表达式搜索替换相关推荐

  1. sublime text 3 中的php代码语法检测

    sublime text 3 中存在一个php 代码语法检测的插件,如果在sublime中编写php代码出现了语法错误,可以在左侧出现红色的提示标志: 如图所示: 那么这样的效果在sublime编辑器 ...

  2. Sublime Text 3中SublimeLinter的使用

    关于Sublime  Text 2中的SublimeLinter的使用网上多如牛毛,基本上不会遇到什么问题,简单的讲一下关于Sublime Text 3中遇到的问题: 1.通过package cont ...

  3. sublime text 3 中改变.vue文件的颜色

    1.按 Ctrl+Shift+P 2.输入install,选择install Package 3.输入vue,选择 vue syntax hightlight 如果上述方法不起作用,可以选择在下面连接 ...

  4. idea 正则表达式搜索替换

    这篇文章主要介绍了idea 正则表达式搜索替换应用详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下 idea 正则表达式搜索替换简单应用 利用正则表达式, ...

  5. Sublime Text 2 中怎样查找scope的名称

    在Sublime Text 2中新增一个snippet时需要在配置文件中输入scope节点的值来限制snippet使用的语法范围(作用域),这个scope的值可以通过以下方法获得: 进入包所在目录,如 ...

  6. C++代码在Sublime Text 3中编译和运行

    Sublime Text 3中编译和运行 前言 以下可以避免因编码问题导致的乱码现象 前言 在数据结构代码的编写中,我选用了优秀的编辑器Sublime Text 3,但是在实现C++代码的编译和运行上 ...

  7. sublime c 语言 编译,默认情况下,将程序编译为Sublime Text 3中的c 14

    我知道我们可以使用g编译器将程序编译为C.但是g编译器默认为98版本.要将其作为C 14运行,我们需要在终端中添加-std = c 14. Sublime Text因其重量轻和功能而被认为是有竞争力的 ...

  8. mysql中加入正则表达式,mysql中的正则表达式搜索

    mysql中的正则表达式搜索 语法: select * from table_name where column_name regexp '正则表达式' 或区分大小写 select * from ta ...

  9. Sublime Text 3中配置编译和运行Java的两个方法

    方法一: 打开Sublime Text 3,依次点击Preference, Browse Packages,在打开的窗口中双击User文件夹,新建文件JavaC.sublime-build,用记事本打 ...

最新文章

  1. X光、CT、核磁、B超的区别,讲得太到位了
  2. [Python Machine Learning] 学习笔记之scikit-learn机器学习库
  3. “SHOT NOTE”新文具,构建虚实之桥
  4. Oracle RAC Failover 详解
  5. 【script】python 使用json模块实现字符串与字典的相互转换
  6. opencv识别圆的代码(转)
  7. wsimport 的使用问题
  8. 数据库SQL语句之外键
  9. RPM包安装卸载命令
  10. java 注解 controller_@Controller注解
  11. vue中使用v-for,出现警告 component lists rendered with v-for should have explicit keys
  12. outlook2016关闭时最小化到任务栏的完美解决方法
  13. vue 和 ssr+nuxt.js 配置环境变量以及pm2进行服务部署
  14. 10个类脑计算最值得关注玩家 |量子位智库报告(附下载)
  15. 常用开关电源芯片大全
  16. 人工智能的春天来了!Tesra超算网络,助力AI开发!
  17. 行路难20210202
  18. 深入编程之QQ盗号核心代码
  19. stallman 征婚
  20. 网站移动适配之Meta标注、移动跳转实用篇 转载

热门文章

  1. 抓包工具tcpdump和tshark
  2. mysql修改默认的存储引擎
  3. Pa2 函数指针 指针函数 分析 (转)
  4. 你的关注,就是我的动力!(第3次改版中)
  5. 死锁、活锁、性能问题
  6. 什么是IDS/IPS?
  7. HUAWEI USG6000系列 NGFW Module V100R001 典型配置案例
  8. 深入理解java中的线程池
  9. 1 Linux系统性能测试与监测工具汇总
  10. Java语言程序设计(一)选择题