Shorthand for matching identifier with Visual Stud

2019-09-11 23:17发布

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

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

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?

1条回答
甜甜的少女心
2楼-- · 2019-09-11 23:50

Let's check:

VS2005-2010:

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

VS2012-2015:

Match an identifier
\b(_\w+|[\w-[0-9_]]\w*)\b
Matches type1 but not &type1 or #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.

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.

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
查看更多
登录 后发表回答