如何使用查找和替换在 Visual Studio Code 中交换两个单词? [英] How to swap two words in Visual Studio Code with a find and replace?

查看:50
本文介绍了如何使用查找和替换在 Visual Studio Code 中交换两个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做的项目有很多 yaml 文件,其中 lat:long: 的所有实例都需要交换,因为数据是标签错误.

例如,以下内容:

- 纬度:-82.645672"长:'44.941747'标题:项目1"- 纬度:-82.645744"长:'44.940731'标题:项目2"- 纬度:-82.645744"长:'44.940731'标题:项目3"- 纬度:-82.646599"长:'44.941441'标题:项目4"

需要看起来像这样:

- 长:-82.645672"纬度:'44.941747'标题:项目1"- 长:-82.645744"纬度:'44.940731'标题:项目2"- 长:-82.645744"纬度:'44.940731'标题:项目3"- 长:-82.646599"纬度:'44.941441'标题:项目4"

我正在努力弄清楚如何在全局范围内交换这两个词.我查看了可用的插件,但它们似乎只适用于您正在编辑的当前文件,并且仅在突出显示几个词时(即像这样一个

The project I'm working on has a number of yaml files, where all the instances of lat: and long: need to be swapped, since the data is incorrectly labeled.

So for instance, the following:

- lat: "-82.645672"
  long: '44.941747'
  title: "Item 1"
- lat: "-82.645744"
  long: '44.940731'
  title: "Item 2"
- lat: "-82.645744"
  long: '44.940731'
  title: "Item 3"
- lat: "-82.646599"
  long: '44.941441'
  title: "Item 4"

Would need to look like this:

- long: "-82.645672"
  lat: '44.941747'
  title: "Item 1"
- long: "-82.645744"
  lat: '44.940731'
  title: "Item 2"
- long: "-82.645744"
  lat: '44.940731'
  title: "Item 3"
- long: "-82.646599"
  lat: '44.941441'
  title: "Item 4"

I'm struggling to figure out how to swap these two words globally. I looked at the plugins that are available, but they only seem to work with the current file you're editing, and when highlighting only a couple of words (i.e. like this one https://marketplace.visualstudio.com/items?itemName=davidmart.swap-word). I was looking into using regex as a possible solution, but can only find ways to reorder words on the same line. Is there a regex that can be used in a find and replace to swap two words that can get applied to all files in a project?

解决方案

Try this regex:

^(-\s+)(lat)(.*)(\n\s*)(long) // I made a small change here

and replace with:

$1$5$3$4$2

See regex101 demo.

This works perfectly fine for me in the find/replace widget but not in the search/replace across files panel. Why? See this "resolved" issue: issue: regex search and replace.

The issue seems to indicate it was provisionally "fixed" but it doesn't appear that it has been.

I was going to open a new issue but found this from earlier this week: issue: capture groups don't work when regex includes newline . So hopefully it will be fixed this iteration.

I am happy to report that this bug has been fixed in the Insiders Build 2019-09-16!! Demo below in Insider's Build:

这篇关于如何使用查找和替换在 Visual Studio Code 中交换两个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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