使用 Visual Studio 正则表达式匹配标识符的简写 [英] Shorthand for matching identifier with Visual Studio regex

查看:27
本文介绍了使用 Visual Studio 正则表达式匹配标识符的简写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 VS2013 开始,VS 使用当时的 .NET regex 语法,这更标准,这是一件好事.

Since VS2013, VS uses then .NET regex syntax, which is more standard and that is a good thing.

但是,我找不到匹配标识符的简写,以前是 :i

However, I haven't been able to find a shorthand for matching identifiers, which previously was :i!

MSDN:i 被替换为 \b(_\w+|[\w-[0-9_]]\w*)\b ...此简短参考.

MSDN says that :i was replaced by \b(_\w+|[\w-[0-9_]]\w*)\b ... so does this short reference.

真的没有更短的版本吗?

Is there really no shorter version?

推荐答案

让我们检查一下:

VS2005-2010:

C/C++ 标识符
:i
表达式的简写 ([a-zA-Z_$][a-zA-Z0-9_$]*).匹配任何可能的 C/C++ 标识符.

C/C++ Identifier
:i
Shorthand for the expression ([a-zA-Z_$][a-zA-Z0-9_$]*). Matches any possible C/C++ identifier.

VS2012-2015:

匹配标识符
\b(_\w+|[\w-[0-9_]]\w*)\b
匹配 type1 但不匹配 &type1#define.

现在,测试 新正则表达式旧正则表达式.结果相同并且都匹配 &type1#define.不知道是不是有意为之.

Now, test the new regex and old regex. The results are the same and both match &type1 and #define. No idea if it is intended.

然而,当谈到缩短时,[\w-[0-9_]] 几乎等于.NET 中的 \p{L}(\w 没有 0-9 和下划线,但仍然匹配一些印地语和一些其他数字).我猜 \p{L} 实际上是这个意思,你可以使用 \b(_\w+|\p{L}\w*)\b.

However, when it comes for shortening, [\w-[0-9_]] is almost equal to \p{L} in .NET (\w without 0-9 and an underscore, but still matching some Hindi and some other digits). I guess \p{L} was actually meant, and you can use \b(_\w+|\p{L}\w*)\b.

如果您不想匹配 &type#define,您需要一个 后视版:

If you do not want to match &type or #define, you need a look-behind version:

\b(?<![&#])(_\w+|\p{L}\w*)\b

或者 - 也许 - 使用 单词前的空格检查:

Or - perhaps - with a whitespace check before the word:

(?<=^|\s)(_\w+|\p{L}\w*)\b

这篇关于使用 Visual Studio 正则表达式匹配标识符的简写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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