VSCode 通配符搜索和替换正则表达式 [英] VSCode wildcard Search and Replace Regex

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

问题描述

我正在尝试进行项目范围的搜索和替换

I'm trying to do a project wide search and replace

来自:

drivers[i].findElement(By.id("elementID")).click();

到:

findAndClick(driver[i], "elementID", true)

问题是 elementID 可以是任何东西,所以我正在尝试通配符搜索并替换为通配符中的内容?

The issue is the elementID can be anything so I'm trying to wildcard search and replace with what's in the wildcard?

推荐答案

这里需要使用 .+? 而不是 * ,因为这使用了正则表达式.

You'll need to use .+? instead of * here since this uses regular expressions.

在正则表达式中,点 . 表示任何字符",加号 + 表示一次或多次",问号 ? 后面的意思是尽量少匹配这个";- 这很有用,因此它不会一直匹配超过您的引号

In regular expressions a dot . means "any character", the plus + means "one or more times", and the question mark ? after this means "try to match this as few as possible times" - which is useful so it won't keep matching past your quote marks

不过,要清楚的是,您必须创建一个有效的正则表达式,这意味着您需要对括号、点等进行转义.

To be clear though, you have to make a valid regex, which means you'll need to escape your parenthesis, dots, etc.

这是完整的解决方案

查找:drivers\[i\]\.findElement\(By\.id\("(.+?)"\)\)\.click\(\);

替换为: findAndClick(driver[i], "$1", true)

注意在通配符"周围添加的未转义括号(.+) 这会在正则表达式中创建一个捕获组,这在替换中转换为 $1 ,因为它是第一个捕获组.

Note the added unescaped parentheses in there around the "wildcard" (.+) this creates a capture group in a regex, which is what translates to $1 in the replacement since it's the 1st capture group.

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

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