vscode和visual studio之间的正则表达式区别 [英] regex difference between vscode and visual studio

查看:71
本文介绍了vscode和visual studio之间的正则表达式区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

vscode 和visual studio 的正则表达式区别

regex difference between vscode and visual studio

开头

line1
line2

查找:^(.+)$替换:$1",

find: ^(.+)$ replace: "$1",

在 vscode 中它按预期工作,导致

In vscode it works as expected, resulting in

"line1",
"line2",

在工作室中,似乎不起作用,导致

In studio, doesn't seem to work, resulting in

"line1
",
"line2
",

哪个是正确的?我假设 vscode.

Which one is correct? I assume vscode.

推荐答案

TL;DR: 使用 ^(.*[^\r\n]) 匹配整行没有 EOL 字符.

TL;DR: Use ^(.*[^\r\n]) to match a whole line without EOL characters.

根据 文档:

<头>
目的表达示例
匹配任意单个字符(换行符除外).a.o 匹配aro";在周围"和abo"在关于"中但不是acro"在跨越"
将匹配字符串锚定到行尾\r?$car\r?$ 匹配car";仅当它出现在行尾时
将匹配字符串锚定到文件末尾$car$ 匹配car";仅当它出现在文件末尾时

然而,由于某些原因,其中一些似乎并不成立(即,. 确实匹配换行符,而 .$ 确实匹配 任何行).以下所有模式将从行首到行尾匹配包括 EOL 字符:^.+, ^.+$,^.+\r?$.

However, some of that doesn't seem to hold true for some reason (i.e., . does match a line break and .$ does match the end of any line). All of the following patterns will match from the beginning to the end of the line including EOL characters: ^.+, ^.+$, ^.+\r?$.

我之前在 VS2017 中注意到了这种行为,我不确定为什么会发生这种情况,但我能够使用以下类似的方法绕过它:

I have noticed this behavior in VS2017 before and I'm not sure why it happens but I was able to get around it using something like the following:

^(.*[^\r\n])

注意:您也可以去掉捕获组并替换为"$0",.

Note: You can also get rid of the capturing group and replace with "$0",.

这篇关于vscode和visual studio之间的正则表达式区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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