使用 Visual Studio 正则表达式匹配标识符的简写 [英] Shorthand for matching identifier with Visual Studio regex
问题描述
从 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?
推荐答案
让我们检查一下:
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.
匹配标识符\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屋!