regex - How to duplicate regex search result within one line?
问题描述
I have a csv table following the scheme:
"text1","text2",3
"text5","text?",5
"baa","foo",99
...
Which I need to transform to:
"text1","text2","-text2-",3
"text5","text?","-text?-",5
"baa","foo","-foo-",99
...
I'm sorry but I have no idea how to duplicate a part of a line using a regex. I'm using VS Code find-replace engine. How could I do this?
解决方案
See regex101 demo.
Find: ^(\s*"[^"]*?","([^"]*?)",)
Replace: $1"-$2-",
Group 1: the first two values in each line, like "text1","text2",
Group 2: just the inner second value, like text2
Replace: Use Group 1 and then replicate Group 2 with surrounding "-Group2-"
Make sure you have this in your settings.json:
"search.usePCRE2": true,
推荐阅读
- javascript - 如何使用引导程序 3 为该行的特定列获取行类的全宽
- kubernetes - Kubernetes 探测事件不可见但失败
- sql - 如何根据最近的日期属性获取两个字段?
- cakephp - 根据控制器禁用 debug_kit
- git - 如何根据最新的 git describe 命令自动设置我的 Inno Setup 安装程序的版本?
- mysql - 在一个大的 mySQL 表上选择会引发所有连接切换到等待处理程序提交的状态
- c - 为什么 ARM CMSIS I2C 读取功能不起作用?
- node.js - npm WARN read-shrinkwrap 这个版本的 npm 兼容 lockfileVersion@1,但是 package-lock.json 是为 lockfileVersion@0 生成的
- c# - UWP 重新启动应用程序列表
- python - 为什么我的画布图像没有使用 Tkinter 和 Pillow 显示?